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)
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)