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

Using an if statement and for loop doesn't work when comparing?

Asked by 4 years ago

I want the for loop to list through all of the items in a model, then for the if statement to check whether that is there. If it is, it will print "Hello". However, the two aren't comparing correctly using the if statement. Here is my code:

local player = game.Players.LocalPlayer.Name
local frame = script.Parent.Frame
local tool = game.Lighting.Crisps

for i,v in pairs(frame:GetChildren()) do
    frame:TweenPosition(UDim2.new(0.113, 0,0.141, 0))
    v.MouseButton1Click:Connect(function()
    local flavour = v
    print(flavour)
        for i,v in pairs(tool:GetChildren()) do
            if v.Name == flavour then
                print("Hello")              
            end
        end     
    end)
end

1 answer

Log in to vote
0
Answered by
Unhumanly 152
4 years ago
Edited 4 years ago

On line 8, you are setting flavour to be equal to v, which is at the time the Instance that the for loop is on.

Let's assume that this Instance is a TextButton. You are setting flavour to an object value, not a string value.

On line 11, you're comparing a string value to an object value.

To fix it, just add a ".Name" to the end of "flavour" on line 11.

local player = game.Players.LocalPlayer.Name
local frame = script.Parent.Frame
local tool = game.Lighting.Crisps

for i,v in pairs(frame:GetChildren()) do
    frame:TweenPosition(UDim2.new(0.113, 0,0.141, 0))
    v.MouseButton1Click:Connect(function()
    local flavour = v
    print(flavour)
        for i,v in pairs(tool:GetChildren()) do
            if v.Name == flavour.Name then
                print("Hello")              
            end
        end     
    end)
end
1
Ah, thanks for the answer and the added explanation. That makes sense as to why it didn't work now. RyanTheLion911 195 — 4y
Ad

Answer this question