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

Randomized death sound script not working?

Asked by 3 years ago

I have a game I'm working on and am trying to make it so upon death, the sound is random. Here's what I have so far:

function Died(p)
    wait(.001)
    local tracks=game.ServerStorage:GetChildren()
    local rn=math.random(1,#tracks)
    local track=tracks[rn]
    track:Play()

Anyone have any idea as to why it's not working? It's a local script in the workspace.

2 answers

Log in to vote
0
Answered by 3 years ago

Your code is attempting to play a sound that is a child of ServerStorage. This will not work. You should move the sound into Workspace (for global sound) or, into the character of the player who has died (local sound)

0
I've moved all the sounds into a folder and put the folder into StarterPlayerScripts so it's in the actual player. I then made it so the script paths to the sounds folder in the player, selects a random sound, and plays it. But still, nothing happens. antaramize 15 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
local SoundAmount = 0
local Sounds = {}

function Died(p)
    SoundAmount = 0
    Sounds - {}
    for _, e in pairs([path of sounds]:GetChildren() do
        SoundAmount = SoundAmount + 1
        table.Insert(Sounds, e)
    end
    local SoundChoice = math.random(1, SoundAmount)
    for i, e in ipairs(Sounds) do
        if SoundChoice == i then
            [path of sounds][e]:Play()
        end
    end
end)

As far as im concerned this should work. You might need to do some testing and debugging.

Answer this question