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

ReplicatedFirst.GameManager:11: bad argument #1 to 'random' (number expected, got table)?

Asked by 6 years ago

I want a random sound but the Output get me this error

ReplicatedFirst.GameManager:11: bad argument #1 to 'random' (number expected, got table)

local GameManager = {
    DeathSounds = {
        131138839,
        131138845,
        131138854,  
    },
}

function zombieDeathSong(z)
    local sound = Instance.new('Sound', z)
    sound.SoundId = 'rbxassetid://'.. math.random(GameManager.DeathSounds)
    sound:Play()
end
1
`GameManager.DeathSounds[math.random(1, 3)]`? TheeDeathCaster 2368 — 6y
0
The error explains itself User#5423 17 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago

You're returning a table, as says kingdom5. To get a random thing inside a table, you do:

sound.SoundId = 'rbxassetid://'..GameManager.DeathSounds[math.random(1,#GameManager.DeathSounds)]

How this works: To access a certain thing inside a table, you do table[number] to access, for example, GameManager.DeathSounds[1] returns 131138839.

0
Thanks:)) NiniBlackJackQc 1562 — 6y
Ad

Answer this question