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
1 year 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:

local user = game:GetService("Players").LocalPlayer
local indiUI = user.PlayerGui.Roll.TextLabel
local what = game.Workspace.lobbySpawn

what.Touched:Connect(function()
    while true do
        wait()
    local numbers = math.random(1, 6)
    if numbers == 1 then
    indiUI.Text = "1"
    elseif numbers == 2 then
    indiUI.Text = "2"
    elseif numbers == 3 then
    indiUI.Text = "3"
    elseif numbers == 4 then
    indiUI.Text = "4"
    elseif numbers == 5 then
    indiUI.Text = "5"
    elseif numbers == 6 then
    indiUI.Text = "6"
        end
    end
end)

0
If you want this to happen when lobbySpawn is touched just tell me! There is an easy way to do it. Sw3atyEgirl 15 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago

I hope this works for you.

local user = game:GetService("Players").LocalPlayer
local indiUI = user.PlayerGui.Roll.TextLabel
local what = game.Workspace.lobbySpawn

local timeToWait = 0.01

while true do -- Looping forever (true will always equal true)
    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
    indiUI.Text = random -- Then set the text of indiUI to it
    wait(timeToWait) -- Then wait
end
0
Thanks it worked R0jym 47 — 1y
Ad
Log in to vote
0
Answered by 1 year ago
local user = game:GetService("Players").LocalPlayer
local indiUI = user.PlayerGui.Roll.TextLabel
local what = game.Workspace.lobbySpawn

local waitTime = 0

while true do
    local random = tostring(Random.new():NextInteger(1,6)) --math.random() is unreliable especially when u constantly select new random numbers
    indiUI.Text = random
    task.wait(waitTime) --task.wait() is just an improved version of wait()
end

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

Answer this question