Why won't it print when value is 1

I am trying to make this script work when testdata changes, but it will print when I joined the game, so I added a fix to this, but now when my value testdata is set at 0 when I join the game and it increases to 1 it won’t print. But if it increases again to 2 then it will print how can I fix this??

local hasPrinted = false

	testdata.Changed:Connect(function()
	if hasPrinted == true then
			print(testdata.Value)
		else 
			hasPrinted = true
		end
			
		
	end)
1 Like

That’s because when you first join and testdata gets changed, hasPrinted is still false, that runs the code in the else statement which sets hasPrinted to true so the next time that testdata is changed, the if statement runs the code as hasPrinted is true and prints the value.

1 Like

Ok, but if I removed hasPrinted stuff when I initially join it will print, but the value hasn’t even changed. how could I fix this?

That is weird, what does it print when you first join?

The current value of testdata. [testdata is a datastore value, so it would be set to 0-20]

This makes sense, when you make an IntValue, in this case testdata, its initial value is set to 0, then you load the saved value of the datastore which changes the value of the IntValue, firing the .Changed event.

1 Like

Oh ok, so if I put a wait in there to wait 10 seconds that way everything is loaded would that fix the problem?

That should work, make sure to put the wait outside of the .Changed event and keep in mind that it may not work sometimes. It’d be a better idea to use a BindEvent to communicate between the two scripts, if you show me your leaderstats script I can help you on how to use a BindEvent in this case.

1 Like

Thanks I will try the bindevent so far the wait 10 does work!!

1 Like

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