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)
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.
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.
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.