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

for _,v In pairs.... What am i doing wrong? Can someone explain it?

Asked by 4 years ago
Edited 4 years ago

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

0
I think you just need to capitalize the 'f' in 'findFirstChild', line 7 Lazarix9 245 — 4y
0
not really, but good catch User#23252 26 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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.

0
Changed, but nothing happens :P HeadlessDeathSpeaker 9 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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
0
Ain't working HeadlessDeathSpeaker 9 — 4y

Answer this question