I’m trying to get this script to print something when the numValue’s value changes to 0 in ReplicatedStorage. The value starts at 5 when you join the game, but it counts down every 1 second down to 0, when you press a button.
I put the server script in ServerScriptStorage and the numValue (named CountdownTime) in ReplicatedStorage. I’m not sure if the problem might be with this, or something else.
When I make it say “if CountdownTime ==” any number below 5, it doesn’t do anything - even when I manually change it in studio when I run the game. Is there something I’m missing to keep checking if it’s 0?
Here’s the script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountdownTime = ReplicatedStorage:WaitForChild("CountdownTime")
if CountdownTime.Value == 0 then
print("Go!")
end
What is happening is when you run the script it is only checking the value once as you only have the if statement one once. Well you might think of running a loop and always checking would would work but is tough on memory. So how do I check if the value changes without a loop you may ask. Roblox has this covered with a useful event called the .changed event. It fires any time the object you assign it to changes at all such as the value changes. you can read the event in depth here https://developer.roblox.com/en-us/api-reference/event/Instance/Changed. I think you know where I am going with this. You would simply use the .changed event to check the value anytime it changes. here is what it would look like.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountdownTime = ReplicatedStorage:WaitForChild("CountdownTime")
CountdownTime.Changed:Connect(function()
if CountdownTime.Value == 0 then
print("Go!")
end
end)
dont forget to mark this as a solution if it works
Also I apologize is if this syntax is wrong as I just made this now
You could also use GetPropertyChangedSignal so that it runs only when the value changes.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountdownTime = ReplicatedStorage:WaitForChild("CountdownTime")
CountdownTime:GetPropertyChangedSignal("Value"):Connect(function()
if CountdownTime.Value == 0 then
print("Go!")
end
end)
that is not needed as any values has only 3-4 properties so using get property changes signal is only making it more complicated and saving little to no memory
I said that because sometimes the name or parent of ValueBase objects could change (very rarely it would happen though).
Edit: I just realized that it only fires for the value in Value objects. Didn’t know that before.
while i dont know what this has to do with the mouse event but either way you should not give people bad advice as he might not know which one to choose and go with the bad idea which is looping forever.
This should be the final code he should use as written by @jakebball2019:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountdownTime = ReplicatedStorage:WaitForChild("CountdownTime")
CountdownTime.Changed:Connect(function()
if CountdownTime.Value == 0 then
print("Go!")
end
end)
I was merely trying to suggest that he keep running the if statement. He doesn’t have to use a while true do if he doesn’t want to, but again, just saying he should keep checking if the condition is met.