value.Changed not working

my script for detecting a value change(serverscript):

game.Lighting.Serverstats:WaitForChild("isplaying").Value = true


game.Lighting.Serverstats.isplaying.Changed:Connect(function()
	if game.Lighting.Serverstats.isplaying.Value == true then
		print("Changed detected: true")
	else
		print("Change detected: false")
		print("waiting 20 sec")
		wait(20)
		game.Lighting.Serverstats.isplaying.Value = true
	end
end)

and this is the main script:

game.Lighting:WaitForChild("Serverstats"):WaitForChild("isplaying").Changed:Connect(function()
	if game.Lighting.Serverstats.isplaying.Value == true then
		game.Lighting.Serverstats:WaitForChild("isplaying").Value = false
--and the rest of the script

so basically the first script triggers the main script’s function and the main script sets the isplaying’s value to “false”, which is SUPPOSED to trigger the value change script(the first one) which would countdown 20 seconds before changing it back to true(and get the mainscript to work again)

but for some reason the first changer → main script trigger works ONCE
then the changer script doesnt respond for this:

game.Lighting.Serverstats:WaitForChild("isplaying").Value = false
2 Likes

are you planning on making this an endless loop?
If you aren’t then this will make an endless loop.
if you are, then the problem is that you’re making a change at the start before priming the functions. You need to set up the function that will work before you start doing changes

function → function → change

try putting this at the end of the code
game.Lighting.Serverstats:WaitForChild(“isplaying”).Value = true
instead of the start

2 Likes

Thank you I’ll try that when I get my first chance.

However,
would that line of code still initiate at the start of the game even if I put it at the bottom?

It is better to use GetPropertyChangedSignal than Changed because Changed Event fires if a single property is changed while GetPropertyChangedSignal Event fires when the GIVEN property is changed.

1 Like

For ValueBase objects Changed only fires for changes in the Value property, since it’s non inherited.

3 Likes

You need to register both changed events before doing your initial change.

The current setup without any yielding anywhere means you change the value, it triggers the event in the second script, which then changes it to false, and then you register the changed event in the first script. So you completely miss the change because it’s registered too late.

4 Likes

So like istun’s reply, your saying that I should move

game.Lighting.Serverstats:WaitForChild("isplaying").Value = true

to the bottom?

Yes pretty much, but bear in mind there’s also a potential issue depending on the order in which your two scripts run. The logic here is quite fragile and is a bit of a race condition. If something happened to your other script delaying it from running you’d end up in a similar situation of missing the event.

Personally I do all my initialisation and then create a value to tell other scripts in the game that the script is initialised and ready. The other scripts wait for this value before doing any actions that interact with that script.

Moving it to the bottom should solve it for now, just keep in mind the processing order in case you encounter similar race conditions in the future.

5 Likes