If statement not working properly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m making a simple script where a value will slowly go up and the script does not print for any numbers BUT 5.

  1. What is the issue? Include screenshots / videos if possible!

The script completely ignores that the value has even reached 5, (Even though it has.)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried changing ‘is equal to 5’ to ‘does not equal 5’ and it printed for every number, including 5.

And for anyone asking, there are no errors in the output.

Here is the code:

while task.wait() do

if game.ReplicatedStorage.Values.CurrentRoom.Value == 5 then
		
		print("No. 5 has been reached.")

end
end)
1 Like

Do you mind explaining how the value is going up? Like does value go up every time a button is pressed?

This could be caused by floating values. I suggest using math.round() on the value before you check to round the number to the closest integer.

Yes, when a button is clicked the value goes up by 1.

Ok, I’ll try this out and see if this works.

This didn’t work as I thought because the value is being added by 1.

Maybe this is a client vs. server problem. If the client is changing the value, the server will not see this.

If this is the case you will need to use remote events.

You can verify this by checking the value in studio in the server view vs client view.

toggle button:
image

1 Like

Everything is handled within the server so this is not the issue.

1 Like

I caught onto that and removed it but it didn’t fix the script.

Yeah, I just realised that from the other posts so I deleted that post. My bad…

Please post the code that changes the value.

The other thing to do when troubleshooting is to print variables before checks.

while task.wait() do
    print(game.ReplicatedStorage.Values.CurrentRoom.Value)  -- tells you what the value actually is
    if game.ReplicatedStorage.Values.CurrentRoom.Value == 5 then	
		print("No. 5 has been reached.")
    end
end)

Figured it out, this entire time I was referencing the wrong number value. :person_facepalming:

That’s kinda what I figured. The reason I asked for your code was to see where you were referencing it from.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.