Hello, I hope you are very well!
In this post I would like to know what is wrong with my code, what I need to do is update the text of my textLabel every time the while do is repeated.
For now, everything works correctly for me except the text of the textLabel, it is only updated the first time and then it does not do it again, what is wrong?
local result = math.random(1, 3)
while result == 1 or result == 2 or result == 3 do
wait(2)
local result = math.random(1, 3)
if (result == 1) then
counter.Text = tostring(result)
print("Green has win!")
wait(3)
end
if (result == 2) then
counter.Text = tostring(result)
print("Red has win!")
wait(3)
end
if (result == 3) then
counter.Text = tostring(result)
print("Blue has win!")
wait(3)
end
end
The code enters all the ifs and shows their prints but does not change the value of the textlabel
local RunService = game:GetService("RunService")
local OldTime = os.time()
local TeamColorTable = {
"Green",
"Red",
"Blue"
}
while true do RunService.Heartbeat:Wait()
if os.time() - OldTime == 5 then
local result = math.random(1, #TeamColorTable)
counter.Text = tostring(result)
print(TeamColorTable[result].." has won!")
OldTime = os.time()
end
end
idk how you defined counter but ima just send this code and see if it helps
Instead of a while do, use a RunService.HeartBeat like @D0RYU showed. Heartbeat fires every frame, so it will loop. You can also use a while true do loop.
Using a loop here is absolutely fine.
You are waiting 5 seconds each time round the loop, there’s really no need to worry about whether this code runs every single frame or not… Some other scenarios the difference between a loop and heartbeat matters, but not here.
I’ve run your code here and it worked OK for me and changed the value of a text label
But I can’t see from what you posted where your counter variable is set up. I’m going to guess that it’s maybe pointing to the wrong thing somewhere?