Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I have a script that changed the value of a variable every second, but why isn't it changing?

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!

1 comment

0

why do you have a changed event inside a infinite loop?

1 answer

✓ Accepted answer

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!

6 comments

0

Your code isn't working. The text (script.Parent.Text) isn't changing. Yes, I check the script is enabled

0

ok, check output.

0

ohhh.. okay some script disabled the script and idk why... thank you

0

Nvm it didn't work. Output was helpful and said absolutely nothing. ¯_(?)_/¯

0

okay NOW i solved it! thank you!

0

Lol k

This discussion is preserved as it was. New answers, comments, and votes are closed.