Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Duksten 20
6 years ago

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)
0
Error:numbers cannot be a string okay? how simple is that hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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

Answer this question