简体   繁体   中英

How do you clone a script in roblox?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM