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

Randomization between several death sounds?

Asked by 9 years ago

I'm trying to edit the player sound script so player death cries are randomized. This current script just doesn’t work. Can this be accomplished with what I have or will I need to go about it in a totally different way?

-- util

function waitForChild(parent, childName)
    local child = parent:findFirstChild(childName)
    if child then return child end
    while true do
        child = parent.ChildAdded:wait()
        if child.Name==childName then return child end
    end
end

function newSound(id)
    local sound = Instance.new("Sound")
    sound.SoundId = id
    sound.archivable = false
    sound.Parent = script.Parent.Head
    return sound
end

-- declarations
IDs = {130965811, 131045862, 131045876} 

local sFallingDown = newSound("rbxasset://sounds/splat.wav")
local sFreeFalling = newSound("rbxasset://sounds/swoosh.wav")
local sGettingUp = newSound("rbxasset://sounds/hit.wav")
local sJumping = newSound("rbxasset://sounds/button.wav")
local sRunning = newSound("rbxasset://sounds/bfsl-minifigfoots1.mp3")
sRunning.Looped = true

local Figure = script.Parent
local Head = waitForChild(Figure, "Head")
local Humanoid = waitForChild(Figure, "Humanoid")

-- functions

function onDied()
    local sDied = newSound("http://www.roblox.com/asset/?id="..tostring(IDs[math.random(1,#IDs)])
    sDied:Play()
end

function onState(state, sound)
    if state then
        sound:Play()
    else
        sound:Pause()
    end
end

function onRunning(speed)
    if speed>0 then
        sRunning:Play()
    else
        sRunning:Pause()
    end
end

-- connect up

Humanoid.Died:connect(onDied)
Humanoid.Running:connect(onRunning)
Humanoid.Jumping:connect(function(state) onState(state, sJumping) end)
Humanoid.GettingUp:connect(function(state) onState(state, sGettingUp) end)
Humanoid.FreeFalling:connect(function(state) onState(state, sFreeFalling) end)
Humanoid.FallingDown:connect(function(state) onState(state, sFallingDown) end)

1 answer

Log in to vote
2
Answered by
TaslemGuy 211 Moderation Voter
9 years ago

The problem with your code is that you're choosing the random sound ID only once. Then, you'll use that same sound forever (it was randomly chosen, after all) but never switch to another.

Simple remove your existing sDeath variable, and change your onDied function to the following:

function onDied()
    local sDied = newSound("http://www.roblox.com/asset/?id="..tostring(IDs[math.random(1,#IDs)])
    sDied:Play()
end

The one potential downside here is that you're making a new sound object every time you want to play one. If you want to avoid that, you could instead do:

(outside of any of your functions, right after defining IDs)

local sounds = { }
for i = 1, #IDs do
    sounds[i] = newSound("http://www.roblox.com/asset/?id="..tostring(IDs[i])
end

And then change your onDied to the following:

function onDied()
    local sDied = sounds[ math.random(1, #sounds) ]
    sDied:Play()
end
0
I'm not getting any sound out of it. I updated my question to reflect the changes I made to the script. The script had a red squiggle under the "sDied" at the end of the onDied function. RobloxBossk 20 — 9y
Ad

Answer this question