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

Script does not run code when a specific array is selected?

I am testing a script that will execute code whenever a player joins the game, and the code ran will be randomly selected from a table using math.random, however when I played it, there is nothing in the output, and the script appears to fail!

local options = {"op1", "op2"}

function option()
	
	local randomOption = math.random(1,#options)		
	
	if randomOption == "op1" then
		print("this")
		
	elseif randomOption == "op2" then
		
		print("that")	
	
	end
		

end

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(option)

1 comment

0

Error:numbers cannot be a string okay? how simple is that

1 answer

✓ Accepted answer

Your problem here is that randomOption is a number, while you are trying to index a table. The simple fix is to say you are trying to index that number in said table

All you need to do is add the fact you are indexing the table, like so:

local options = {"op1", "op2"}

function option()	
	local randomOption = math.random(#options)        

	if options[randomOption] == "op1" then
		print("this")
	elseif options[randomOption] == "op2" then
		print("that")
	end
end

game:GetService("Players").PlayerAdded:Connect(option)

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