Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

:Clone() is cloning the wrong thing I am not sure if it is a glitch or not? [SOLVED]

Asked by 3 years ago
Edited 3 years ago

So I am not the best at scripting but not too bad either and I am a bit confused why when I clone the script then destroy it it copies the wrong script more context: I am making a simulator where you can buy clones but I just cant get my head around how tags work so basically this script is in a dummy something the clone will kill but I don't know how to use tags so I make it clone itself then destroy itself can someone please tell me if this will work and if it wont what's the problem?

> `local Humanoid = script.Parent.Humanoid
function Dead()
    local clone = script.Parent.Parent.Parent.Folder:GetChildren()
    for _, v in pairs(clone) do
        local name = v.Name
        local plr = game.Players:FindFirstChild(name)
        plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + game.ServerStorage["CloneVal's"].CashVal.Value
        plr.leaderstats.Kills.Value = plr.leaderstats.Kills.Value + 1
        local scriptclone = script:Clone()
        scriptclone.Parent = script.Parent
        scriptclone.Name = "MoneyScriptClone"
        scriptclone.Disabled = false
        script:Destroy()
    end
end
Humanoid.Died:Connect(Dead)
`

so basically there is a script (the script above) in a folder in workspace that gets copied into this dummy that a clone of the player would kill and each time it gives the player money so I have to find the clones name then find it as a player then give the player leaderstats but I have recently figured out that it will only work once because the humanoid or character resets and the best way is to use tags but I cant get my head around them so I make the script destroy itself after cloning itself but instead what it does is clones the script to make it respawn but what makes me think it is a glitch is that it keeps cloning the wrong script when the dummy dies even though the script that clones itself is gone and it cant be cloned by the script that gets cloned in the first place because it is deactivated and only the clone gets activated.

all help is appreciated

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The word 'script' is a so called keyword in Roblox lua, this means that it's always available regardless of context. The script keyword is used to reference the script which runs the code, so for example using script.Parent.BrickColor = BrickColor.new("Really red") would change the color of the parent of the script.

All of this means that keywords shouldn't be used as variable names, as in your case it would prioritize the keyword over the variable name, thus cloning the script which runs the code and not the script you want to target.

You could rename the 'script' variable to something like 'script_' or 'script0' or anything which isn't just 'script'

0
I will try that  terraria265 5 — 3y
0
It didn't work. The variable name is "scriptclone" and I changed it to "clone2" and nothing changes. terraria265 5 — 3y
0
I might've been a bit unclear. You should call the variable in the script something different. So instead of having a variable named 'script' you could use 'script2'. The one on line 9 which is just called 'script' Vinceberget 1420 — 3y
0
The script I want to clone is the script that runs it but instead it clones the wrong script. I might just change it so instead of using script I will actually get it from game.Workspace terraria265 5 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Sorry if I wasted some of your time but the answer was actually that it was a different script that was breaking it so all I have done is merged them together

Answer this question