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

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

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)

1 comment

0

If you want this to happen when lobbySpawn is touched just tell me! There is an easy way to do it.

2 answers

✓ Accepted answer

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

1 comment

0

Thanks it worked

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

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