Reputation: 1
Is it possible to clone Scripts in Roblox? I've tried for a bit and getting no errors but it's not working.
while task.wait() do
if script.Parent.Parent.Scripts.BackgroundColor3 == Color3.new(0.466667, 0.866667, 0.466667) then
local clonething = script.Parent.Parent.Scripts.CloneDis
local part = script.Parent.Parent.Scripts.CloneDis:Clone()
print('ea') -- this prints in the console but nothing is cloned to the folder
part.Parent = clonething.Parent.ScriptFolder
end end
I am trying to clone inside of a ui if that helps.
Upvotes: 0
Views: 1677
Reputation: 7188
You mentioned this code was in a GUI so this is likely a LocalScript, but the code you're cloning I'm assuming is an actual Script.
It's important to know that Scripts only execute in the Workspace and in ServerScriptService. Your gui exists as a child inside Player > PlayerGui. So if you are cloning a Script from one folder of your gui to another, it won't matter that the cloning worked, because the Script won't execute anyways. The fix is simply to set the Parent of the Script to somewhere in the Workspace or ServerScriptService.
local targetFolder = game.Workspace
local scripts = script.Parent.Parent.Scripts
local activateColor = Color3.new(0.466667, 0.866667, 0.466667)
-- when the color changes, check if we should clone
scripts:GetPropertyChangedSignal("BackgroundColor3"):Connect(function(newColor)
if newColor == activateColor then
local part = scripts.CloneDis:Clone()
part.Parent = targetFolder
print("cloned to Workspace")
end
end)
Also, when you make changes to the world in a LocalScript (like cloning an object), those changes are isolated to your client. They do not then replicate back to the server and also to the other players. So if your Script is supposed to do stuff in the game that other players can see, you need to make those changes from a Script. You should look up RemoteEvents if that's the case.
Upvotes: 2