Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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!

0
why do you have a changed event inside a infinite loop? theking48989987 2147 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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!

0
Your code isn't working. The text (script.Parent.Text) isn't changing. Yes, I check the script is enabled Chez_Guy 67 — 5y
0
ok, check output. WideSteal321 773 — 5y
0
ohhh.. okay some script disabled the script and idk why... thank you Chez_Guy 67 — 5y
0
Nvm it didn't work. Output was helpful and said absolutely nothing. ¯\_(?)_/¯ Chez_Guy 67 — 5y
View all comments (2 more)
0
okay NOW i solved it! thank you! Chez_Guy 67 — 5y
0
Lol k WideSteal321 773 — 5y
Ad

Answer this question