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

How do I fix this if then statement?

Asked by 9 years ago

So this if then statement is supposed to detect a certain value. However, I want it to do something else when the value changes.

local Hit = script:waitforchild("Random")

while wait() do
if Hit.Value == true then
print('Hi')
end
if Hit.Value == false then
print('Goodbye')
end
end

I want the script to do something else once the value changes. How would I make the script do this? Can someone please help me?

1
It is suppost to be WaitForChild. Caps do matter in Lua. woodengop 1134 — 9y

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Continent is correct, you should be using WaitForChild and not waitforchild.

To catch when an Value object is changed, use the Changed event. You can also make that if statement more efficient by using else instead of a second if!

local Hit = script:WaitForChild("Random")

Hit.Changed:connect(function()
    if Hit.Value == true then --the `== true` can be omitted here, since `if true == true then` --> `if true then`
        print("Hi")
    else --If it's not true, it must be false!
        print("Goodbye")
    end
end)

If you want to add more variables to check:

local Hit = script:WaitForChild("Random")

Hit.Changed:connect(function()
    if Hit.Value and someOtherValue.Value then
        print("Hi -- someOtherValue is true!")
    elseif Hit.Value then
        print("Goodbye -- someOtherValue is false!")
    else
        print("someOtherValue is unknown!")
    end

    --or, just add another `if` tree:

    if someThirdValue.Value then
        print(1)
    else
        print(2)
    end
end)
0
Let's say there were two variables involved, how would I get that to work? Would I have another ".Changed:Connect(function()" line? CoolJohnnyboy 121 — 9y
0
No need! You can simply add more `if` trees inside the connected function. I'll edit my answer to include an example. adark 5487 — 9y
Ad

Answer this question