If Statement Not Working When Referring To NumValue In ReplicatedStorage

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

Thanks for any help!

Try printing before the if statement to verify the Value exists

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 :grin:

Also I apologize is if this syntax is wrong as I just made this now

Its better to just use the VAL argument of .Changed.

CountdownTime.Changed:Connect(function(val)
   if val == 0 then
	   print("The Value is now 0!")
   end
end)

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)

Theres not really a difference in that though, just making the script longer. .Changed would be better

Changed gets the property that changed. You should check whether the property that changed was the Value property

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

Why? Changed only fires for the Value property for ValueBase objects since it’s not inherited, so it’s the same thing in this case.

Even if the parent or any property other than the value changes it won’t fire for them, what are you talking about?

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.

Try updating the value in the script in a while true do loop.

Or put the if statement in a while true loop and if the condition is met you can break the loop after the print.

VERY bad idea, that would take up memory for now reason when you can just use the .changed event instead to only fire when needed.

Just a reference. You could also do this on a mouse button 1 down event

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)

Edit: @jakebball2019 exactly

thats what I put in my post above

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.

There is no reason that this topic should be kept alive, as we’re just arguing.

thats not the point, the point is you suggesting a bad and inefficient method does nothing but cause damage to what he is trying to achieve

I will refrain myself from giving bad advice from now on. I will stay more specific to my point and good and bad methods.