How would I make a table of death sounds?

Kind of relating to my last post, I’m trying to make a table of sounds for when a character dies. The LocalScript is in (should be) the correct spot, in StarterPlayer > StarterPlayerScripts. Here’s the script so far:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local Players = game:GetService("Players")
		local Humanoid = char:WaitForChild("Humanoid")
		local DeathSounds = game.Workspace.DeathsFolder:GetChildren()
		local newlocation = DeathSounds.Parent == game.Workspace:WaitForChild("" ..player.Name).HumanoidRootPart
		local randomSound = newlocation.DeathSounds[math.random(1, #newlocation.DeathSounds)]
		local death1 = newlocation.JackeryzDeath1
		local death2 = newlocation.JackeryzDeath2
		local death3 = newlocation.JackeryzDeath3
		
		Humanoid.Died:Connect(function()
			randomSound:Play()
			
			if death1.Playing == true then
				wait(.29)
				death1:Stop()
				death1.TimePosition = 1.34
			end
			if death2.Playing == true then
				wait(.27)
				death2:Stop()
				death2.TimePosition = 2.64
			end
			if death3.Playing == true then
				wait(.25)
				death3:Stop()
				death3.TimePosition = 4.55
			end
		end)
	end)
end)

There are no errors or anything, so I’m not sure what’s going wrong. so ya i need help!!!

6 Likes

Try using

local function onDeathSoundPlayed(sound)
    if sound == death1 then
        wait(.29)
        death1:Stop()
        death1.TimePosition = 1.34
    elseif sound == death2 then
        wait(.27)
        death2:Stop()
        death2.TimePosition = 2.64
    elseif sound == death3 then
        wait(.25)
        death3:Stop()
        death3.TimePosition = 4.55
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local Players = game:GetService("Players")
        local Humanoid = char:WaitForChild("Humanoid")
        local DeathSounds = game.Workspace.DeathsFolder:GetChildren()
        local newLocation = DeathSounds.Parent == game.Workspace:WaitForChild("" .. player.Name .. "").HumanoidRootPart
        local randomSound = newLocation.DeathSounds[math.random(1, #newLocation.DeathSounds)]
        
        Humanoid.Died:Connect(function()
            randomSound:Play()
            onDeathSoundPlayed(randomSound)
        end)
    end)
end)
2 Likes

This wouldn’t work though because the local’s for the death1,2,3 are going to be in the player.Name’s HumanoidRootPart, and the player part is only is the part of the code where the PlayerAdded is. Sorry if I explained that poorly.

1 Like

Thank you for letting me know, i’ve tried revising it, and here is what i made.

local function onDeathSoundPlayed(sound)
    if sound == death1 then
        wait(.29)
        death1:Stop()
        death1.TimePosition = 1.34
    elseif sound == death2 then
        wait(.27)
        death2:Stop()
        death2.TimePosition = 2.64
    elseif sound == death3 then
        wait(.25)
        death3:Stop()
        death3.TimePosition = 4.55
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local Players = game:GetService("Players")
        local Humanoid = char:WaitForChild("Humanoid")
        local DeathSounds = game.Workspace.DeathsFolder:GetChildren()
        local newLocation = DeathSounds.Parent == game.Workspace:WaitForChild("" .. player.Name .. "").HumanoidRootPart
        local randomSound = newLocation.DeathSounds[math.random(1, #newLocation.DeathSounds)]
        
        Humanoid.Died:Connect(function()
            randomSound:Play()
            onDeathSoundPlayed(randomSound)
        end)
    end)
end)
2 Likes

This doesn’t solve the problem

I think what OP means is that all the values for the waits and stuff should be put into a table and referenced.

Like this:

onDeath = {
    death1 = {
        wait = 0.29;
        timepos = 1.34
    };
    -- etc...
}
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local Players = game:GetService("Players")
        local Humanoid = char:WaitForChild("Humanoid")
        local DeathSounds = game.Workspace.DeathsFolder:GetChildren()
        local newLocation = DeathSounds.Parent == game.Workspace:WaitForChild("" .. player.Name .. "").HumanoidRootPart
        local randomSound = newLocation.DeathSounds[math.random(1, #newLocation.DeathSounds)]
        
        Humanoid.Died:Connect(function()
            randomSound:Play()
             
            task.wait(onDeath[randomSound].wait)
            randomSound:Stop()
            randomSound.TimePosition = onDeath[randomSound].timepos
        end)
    end)
end)
2 Likes

Ok, I’ve figured out one way on way this isn’t working as well.
The folder (local DeathSounds) doesn’t move over to the HumanoidRootPart. I know this is totally out of the blue but I would not know why it’s doing this as I’ve done this in other scripts before.

1 Like

Because DeathSounds is a table and not an Instance

Plus, newLocation is a boolean and not an Instance too

Plus plus, you cannot call length of an Instance, I think you meant to put #newLocation.DeathSounds:GetChildren()

1 Like

Ok, I’ve seen what I’ve done wrong but it still doesn’t bring the folder over to the HumanoidRootPart. Here’s the script so far:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local Players = game:GetService("Players")
		local localplayer = Players.LocalPlayer
		local character = localplayer.Character
		local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		local Humanoid = character:FindFirstChild("Humanoid")
		local DeathSounds = game.Workspace.DeathsFolder
		local newLocation = DeathSounds.Parent == HumanoidRootPart
		local randomSound = HumanoidRootPart.DeathSounds[math.random(1, #HumanoidRootPart.DeathSounds:GetChildren())]

		Humanoid.Died:Connect(function()
			randomSound:Play()

			task.wait(onDeath[randomSound].wait)
			randomSound:Stop()
			randomSound.TimePosition = onDeath[randomSound].timepos
		end)
	end)
end)
1 Like

Still a boolean

chaaaarrrsssssss

1 Like

I know what a boolean is, but I still don’t understand what you want me to do. sorry for the confusion : /

1 Like

There’s no point in defining a new variable if you don’t use it. If you just want to change the location of the folder to the HumanoidRootPart then just do:

DeathSounds.Parent = HumanoidRootPart

Try this:

-- // Services
local Players = game:GetService("Players")

-- // Variables
local DeathSounds = {
	{Id = "", WaitTime = 0.29, TimePosition = 1.34},
	{Id = "", WaitTime = 0.27, TimePosition = 2.64},
	{Id = "", WaitTime = 0.25, TimePosition = 4.55},
}

-- // Functions
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		-- Set the random seed based on os.time()
		math.randomseed(math.random(-os.time(), os.time()))
		
		-- Generate random number and get the sound
		local randomNumber = math.random(1, #DeathSounds)
		local randomSound = DeathSounds[randomNumber]

		humanoid.Died:Connect(function()
			
			-- Loop through all objects in the DeathSounds array
			for index, sound in ipairs(DeathSounds) do
				
				-- If the index of the sound is equal to the random number
				if index == randomNumber then
					-- Create new sound and parent it to the head
					local newSound = Instance.new("Sound")
					newSound.SoundId = sound.Id
					newSound.Parent = character:WaitForChild("Head")
					
					newSound:Play()
					
					-- Wait the set amount of time (task.wait() is faster and more reliable than wait())
					task.wait(sound.WaitTime)
					
					newSound:Stop()
					newSound.TimePosition = sound.TimePosition
				end
				
			end

		end)
	end)
end)

Resources:

@TaxFraudBruh @theymightbelias These past 2 replies still don’t work for some reason.

It is important to note that having a PlayerAdded() listener in a LocalScript will not fire the event for the player running the LocalScript. This is mainly to do with the absolute simultaneous creation of the client-side at the same time PlayerAdded() is also fired. Hence, your code will never run past the PlayerAdded() connection only for yourself. Other connecting clients (other than yourself) should be correctly detected by your LocalScript.

Keeping the above point in mind and a partially re-written code, something along the lines of this should get you the intended result:

-- // Services
local Players = game:GetService("Players")

-- // Variables
local LocalPlayer = Players.LocalPlayer
local DeathSounds = {
	{Id = "0000000000", WaitTime = 0.29, TimePosition = 1.34},
	{Id = "1111111111", WaitTime = 0.27, TimePosition = 2.64},
	{Id = "2222222222", WaitTime = 0.25, TimePosition = 4.55},
}

-- // Functions
local function onDeath(plr)
	if plr then
		local char = plr.Character
		
		if char then
			local humanoid = char:WaitForChild("Humanoid")
			
			math.randomseed(math.random(-os.time(), os.time()))
			local randomSound = DeathSounds[math.random(1, #DeathSounds)]
			
			local newSound = Instance.new("Sound")
			newSound.SoundId = "rbxassetid://"..randomSound.Id
			newSound.Name = "DeathSound"
			newSound.Parent = char:WaitForChild("Head")
			
			if not newSound.isLoaded then
				newSound.Loaded:Wait()
			end

			newSound:Play()
			
			task.wait(randomSound.WaitTime)
			
			newSound:Stop()
			newSound.TimePosition = randomSound.TimePosition
		end
	end
end

LocalPlayer.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid")
	
	humanoid.Died:Connect(function()
		onDeath(LocalPlayer)
	end)
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		humanoid.Died:Connect(function()
			onDeath(player)
		end)
	end)
end)

EDIT:
I’m a bit unsure about your WaitTime and TimePosition hardcoding. What exactly are you trying to achieve with waiting a specific amount of time? Is the WaitTime different from the sound’s actual length? If they’re the same, you can simply task.wait(newSound.TimeLength).

Secondarily, your TimePosition also seems redundant due to the fact that it will affect nothing about the sound or the way its played. You’re setting it’s position after it has stopped playing and the player then respawns, resulting in all Sound descendants to the character’s Head to be destroyed, which means your setting of TimePosition causes redundancy.
What are you intending for it to do here?

I can see how that would fix the problem, but nothing has changed. The newSound 's Parent still does not transfer over to the player’s character.

I’d like you to elaborate here a bit please. First of all, by player, are you referring to yourself (LocalPlayer) or other clients? Additionally, how are you testing this?

If there are no errors in sight, it can be significantly helpful to do some basic debugging to check how far does the code go until the intended task derails. This can be anything along the lines of print()ing certain logical parameters or strings at places to assure all obtained values and instances exist, and are correctly assigned.

Oops, that was a messup on my part. The sound does appear into the character (sorry!) but the sound still doesn’t do the :Play() thingy, so you can’t hear it. No errors still.
sorry for the confusion again!

Nevermind, I figured it out. It’s because of the newSound.Loaded:Wait() and the Wait() would just last forever. I know you need to load a sound for it to actually play, so I’ll figure that out myself. So yeahhhhhh. (@ZEDDxR)

1 Like

What are you talking about loading the sound?

The marked solution is quite irrelevant to the original topic. However, I believe they were referring to my added sanity check for the sound’s Loaded state, which is known to cause issues.