So, i wanted a script that removes all targets and replaces them with a :Clone()
This is my script
wait() local NewScript = script.LightScriptV2 wait() for _,v in pairs(workspace:GetChildren()) do wait() if v:findFirstChild("LightScript") then NewScript:Clone() NewScript.Parent = v.Parent print(NewScript.Parent) v:Destrioy() print("Changed. "..v.Name) else print("Not changed "..v.Name) end end
It keeps printing the print("Not changed "..v.Name) line, instead of replacing.
The object he needs to find are in different models in Workspace.
Can someone explain me what i'm doing wrong?
EDIT: I CHANGED THE SCRIPT AS RECOMMEND BELOW.
But still, it doesn't do anything...
(the new script recommended by users in comments) wait() local NewScript = script.LightScriptV2 wait() for _,v in pairs(workspace:GetChildren()) do wait() if v:FindFirstChild("LightScript") then NewScript:Clone().Parent = v v:Destroy() print("Changed. "..v.Name) else end end
On line 11, you have v:Destrioy(), For 1. Make sure it's v:Destroy() Other than that I can't see anything wrong, other than maybe line 7 where the first f in FindFirstChild is lower case.. I've been told that it matters, and also been told it doesn't matter, so IDK.
well, its gonna parent new script to workspace because of the following line of code
NewScript.Parent = v.Parent
also the following part of code could be problematic (but i am not 100% certain)
NewScript:Clone() NewScript.Parent = v.Parent
I'd recommend changing it to
NewScript:Clone().Parent = v
so the main error in your code is that all of the new scripts will be parented to workspace
because of the following line of code
NewScript.Parent = v.Parent
keep in mind that your looping through workspace, therefore, if you do v.Parent
then your referring to workspace
also by doing v:Destroy()
your destroying everything in workspace since your looping through workspace, it should be v.LightScript:Destroy()
here is the modified code:
wait() local NewScript = script.LightScriptV2 wait() for _,item in pairs(workspace:GetChildren()) do if item:FindFirstChild("LightScript") then item.LightScript:Destroy() NewScript:Clone().Parent = item end end