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

Randomized death sound script not working?

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

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)

1 comment

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.

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.

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