I couldn't fit the entire question in the title, so here's the full question:
I have a script that every few seconds it changed a variable (IntValue
) and the value of that variable is shown on a TextLabel
, and whenever the variable changes, it updates the text in said TextLabel
. But the TextLabel
isn't changing.. why?
Okay, that's my question. Here's my code:
local pointsToEarn = script.Parent.PointsToGive local waitTime = script.Parent.PointsPerSecond local pointsVar = script.Parent.Points while true do wait(waitTime.Value) --Default: 2 second pointsVar.Value = pointsVar.Value + pointsToEarn.Value pointsVar.Changed:Connect(function() script.Parent.Text = pointsVar.Value if pointsVar.Value <= -1 then pointsVar.Value = 0 error("Anonymous script/code line attempted to reach negative points. Find bug ASAP") end end) end
(P.S. This script used to work, but now it suddenly doesn't)
Variable values that were set by default:
pointsToEarn Value: 0 (but it changes to 15 later on in another script irrelevant to this question)
waitTime Value: 2 (it waits 2 seconds for the points value below for it to change)
pointsVar Value: 2000 (this is the points value)
The text in the TextLabel
is similar to pointsVar
. And the text changes whenever pointsVar
value changes. Or, that's what I wanted it to do.
If you need any more information, please do not hesitate to ask for it. Thank you!
Your code:
local pointsToEarn = script.Parent.PointsToGive local waitTime = script.Parent.PointsPerSecond local pointsVar = script.Parent.Points while true do wait(waitTime.Value) --Default: 2 second pointsVar.Value = pointsVar.Value + pointsToEarn.Value pointsVar.Changed:Connect(function() script.Parent.Text = pointsVar.Value if pointsVar.Value <= -1 then pointsVar.Value = 0 error("Anonymous script/code line attempted to reach negative points. Find bug ASAP") end end) end
Your problem:
I don't see why you have a changed event inside an infinite loop by what I know I'm pretty sure that will brake the code.
Fixed code:
local pointsToEarn = script.Parent.PointsToGive local waitTime = script.Parent.PointsPerSecond local pointsVar = script.Parent.Points while true do wait(waitTime.Value) --Default: 2 second pointsVar.Value = pointsVar.Value + pointsToEarn.Value end pointsVar.Changed:Connect(function() script.Parent.Text = pointsVar.Value if pointsVar.Value <= -1 then pointsVar.Value = 0 error("Anonymous script/code line attempted to reach negative points. Find bug ASAP") end) end
Any problems with my code? Comment!