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

How to fix Text not changing with math.random and while loop?

Asked by
R0jym 47
2 years ago

Hello there, I tried to make a TextLabel where the text constantly changes it's number with the math.random script, which I obviously used the while loop to make it constantly change without stopping, however it doesn't seem to work, the TextLabel's Text isn't changing at all, how do I fix it?

SCRIPT:

01local user = game:GetService("Players").LocalPlayer
02local indiUI = user.PlayerGui.Roll.TextLabel
03local what = game.Workspace.lobbySpawn
04 
05what.Touched:Connect(function()
06    while true do
07        wait()
08    local numbers = math.random(1, 6)
09    if numbers == 1 then
10    indiUI.Text = "1"
11    elseif numbers == 2 then
12    indiUI.Text = "2"
13    elseif numbers == 3 then
14    indiUI.Text = "3"
15    elseif numbers == 4 then
View all 23 lines...
0
If you want this to happen when lobbySpawn is touched just tell me! There is an easy way to do it. Sw3atyEgirl 15 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

I hope this works for you.

01local user = game:GetService("Players").LocalPlayer
02local indiUI = user.PlayerGui.Roll.TextLabel
03local what = game.Workspace.lobbySpawn
04 
05local timeToWait = 0.01
06 
07while true do -- Looping forever (true will always equal true)
08    random = tostring(math.random(1,6)) -- creating a variable called random. I get a random number from one to six then convert it to a string
09    indiUI.Text = random -- Then set the text of indiUI to it
10    wait(timeToWait) -- Then wait
11end
0
Thanks it worked R0jym 47 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
01local user = game:GetService("Players").LocalPlayer
02local indiUI = user.PlayerGui.Roll.TextLabel
03local what = game.Workspace.lobbySpawn
04 
05local waitTime = 0
06 
07while true do
08    local random = tostring(Random.new():NextInteger(1,6)) --math.random() is unreliable especially when u constantly select new random numbers
09    indiUI.Text = random
10    task.wait(waitTime) --task.wait() is just an improved version of wait()
11end

i just modified @Sw3atyEgirl's code a lil bit, credits to her

Answer this question