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

Using variables inside an i, v in pairs loop does not seem to work?

Asked by 5 years ago
Phase = 1
for i, v in pairs (script.Parent:GetChildren()) do
    if v.Name == Phase then
        v.Visible = true
    end
end

If I replace the "If name == phase then" to if name == "1" then" it works. It just won't work with variables. Anyone know why?

2 answers

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

You're trying to compare a string and a int value together. The reason why Name == "1" works is because of this. "1" is the string. You'll need to change the value of Phase to a string or use tostring() function to convert.

local Phase = "1" -- Either do this or use tostring()

for i, v in pairs (script.Parent:GetChildren()) do
    --The property .Name gives you a string
    if v.Name == Phase then
        v.Visible = true
    end
end


0
Thanks! Thepoint13 99 — 5y
Ad
Log in to vote
0
Answered by
Tizzel40 243 Moderation Voter
5 years ago

The word Phase needs to be a string like this

if v.Name == "Phase" then --all ways put those quotation marks around 

the word a string

Answer this question