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

How can I make a Text box change according to values of an array?

Hello. I currently have a question. I am trying to create a textbox that changes its text, that I put into an array. The text randomly changes. I made this code:

text = {"Cookie", "Soda", "Cake", "Chips", "Doritos"}
script.Parent.Text = text
function randomizer()
	local number = math.random(5)
	local last_number = number
	if number == last_number then
		number = math.random(5)		
	end
	print (text)
	wait(2)
end
randomizer()

However, when I run the code, I get this error:

18:23:13.526 - Workspace.Part.SurfaceGui.TextBox.Script:2: bad argument #3 to 'Text' (string expected, got table)

Can anyone help me with this?

1 answer

✓ Accepted answer

One, I have no idea what the randomizer is meant to do, it just makes a random number. Two, why did you try to set the text to a table?

Anyway, here is the fix hopefully (not tested)

local last_number = 0

text = {"Cookie", "Soda", "Cake", "Chips", "Doritos"}
script.Parent.Text = ""
function randomizer()
	local number = math.random(#text)
	check(number)
	local entry = text[number]
	script.Parent.Text = entry
	print(entry)
end

function check(number)
	if number == last_number then
		number = math.random(#text)	
	end
	if number == last_number then
		check(number)
	end
end

while true do
	randomizer()
	wait(2)
end

Hopefully that does it. :)

2 comments

0

Edited, attempted to run check with a nil value of 'number'. Dumb me..

0

It worked! Thank you very much :)

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