EndeX
EndeX

Reputation: 7

Roblox script doesn't detect a value

I tried to make a script that plays a sound when speed is 100, but it didn't do anything. Script:

local speed = script.Parent.Speed


wait(3)
if speed.Value == 100 then
    print('test')
    workspace.Sound:Play()
end

Upvotes: 0

Views: 597

Answers (2)

Bill Nye
Bill Nye

Reputation: 1

You are reading it at an exact moment, so unless you have other code making it after 3 seconds, then it will pass and not detect!

Upvotes: -2

Kylaaa
Kylaaa

Reputation: 7204

Your code is saying, hey find this IntValue, and after 3 second, if the value is 100, do a thing... and that's it. Once it makes it to the end of the script, it's done.

If you want to listen for changes, and react to those changes, you need to add event listeners to the signals exposed on the IntValue object itself. Roblox provides a Changed signal that will fire every time the object changes.

local speed = script.Parent.Speed
speed.Changed:Connect(function(newValue)
    print("speed is now ", newValue)
    if speed.Value == 100 then
        print("speed is at 100!")
        workspace.Sound:Play()
    end
end)

Upvotes: 3

Related Questions