Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Randomization between several death sounds?

I have a script that gives a custom death sound when a player dies. I would however, like it to work with six or so different sounds at random. Can this be accomplished with what I have or will I need to go about it a totally different way?

This is the parent script.

game.Players.PlayerAdded:connect(function(p)
	p.CharacterAdded:connect(function(character)
		repeat wait() until character ~= nil
		character.Sound:Remove()
		local s = script.Sound:Clone()
		s.Parent = character
		s.Disabled = false
	end)
end)

And this is its child.

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

local sDied = newSound("http://www.roblox.com/asset/?id=130826746")
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()
	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)

Here are the other death sound IDs I was considering adding. 130965811 131045862 131045876 131045905 131045925

1 answer

P = script.
Character.Humanoid

while true do
script.Parent.Visible = false
if P.Health == 0 then
	wait(1.2)
script.Parent.Visible = true
 end
   wait(2)
    end

This discussion is preserved as it was. New answers, comments, and votes are closed.