How do I assign a number value to these 2 sounds to then have it randomly picked

I am trying to make it so on player died either one of the sounds play, and is different each time. The script I’m working with I found on YT but I had to do some small changes then of course I’m trying to add more “funtion” to it. Now I’m still learning but tried to figure it out on my own after some different things I tried this is my final thing. (original script at the bottom

local Root =script.Parent:WaitForChild("HumanoidRootPart")
local DeathSound = script.Parent:WaitForChild("HumanoidRootPart"):WaitForChild("Died", 2)
local JumpSound = script.Parent:WaitForChild("HumanoidRootPart"):WaitForChild("Jumping", 5)
local DeathSound1 = "rbxassetid://4596891584"      
local DeathSound2 = "rbxasetid://607648195"


if DeathSound then 
	DeathSound1 = 1
	DeathSound2 = 2
	DeathSound.SoundId = math.random(1, 2)
	DeathSound.Volume = 1.6
	
	if JumpSound then
		JumpSound.SoundId = "rbxassetid://268933900"
		JumpSound.Volume = 2.3
	end
end

Its not bringing up an error with the script its self, but there is a ouput error Failed to load sound 1: Temp read failed. and no sounds happen when I reset my character. before doing this with the original script it worked, so not sure how to go about adding multiple sounds that is selected at random.
Ideally I’d use the RbxCharacterSounds script but for what ever reason when I copy it, change the ids and put it where it needs to go, all sounds don’t work, this way is so far so its what I’m going with. Also I’d like to do this for more then one type of sound not just the “Died” sound.
In the mean time I’ll keep playing around with it.

Original Script
local Root =script.Parent:WaitForChild("HumanoidRootPart")
local DeathSound = script.Parent:WaitForChild("HumanoidRootPart"):WaitForChild("Died", 2)
local JumpSound = script.Parent:WaitForChild("HumanoidRootPart"):WaitForChild("Jumping", 5)

if DeathSound then 
        DeathSound.SoundId = "rbxassetid://4596891584"   
        DeathSound.Volume = 1.6   

if JumpSound then
		JumpSound.SoundId = "rbxassetid://268933900"
		JumpSound.Volume = 2.3
	end
end

I did try looking online some but its not really for this use case

Try putting the two sound variables into a table and doing something like this: DeathSounds[math.random(1, #Deathsounds)]:Play()

Tables are something I still gotta learn more of but I’ll look into that and see what I can find and or come up with and try that

Hey! I took a look at your script, and the issue is with how you’re selecting and assigning the sound IDs.

I moved the sound IDs into a table (DeathSoundIDs), so it’s easier to manage more sounds in the future. The getRandomSoundID() function randomly selects a sound ID from the table. And I added DeathSound:Play() and JumpSound:Play() to make sure the sounds actually play.

Now, each time the player dies, a random sound from the list will be played. If you want to add more sounds, just add their IDs to the DeathSoundIDs table.

Let me know if this works or if you need more help! :slight_smile:

local Root = script.Parent:WaitForChild("HumanoidRootPart")

local DeathSound = script.Parent:WaitForChild("HumanoidRootPart"):WaitForChild("Died", 2)
local JumpSound = script.Parent:WaitForChild("HumanoidRootPart"):WaitForChild("Jumping", 5)

local DeathSoundIDs = {
    "rbxassetid://4596891584",  -- sound 1
    "rbxassetid://607648195"    -- sound 2
}

local JumpSoundID = "rbxassetid://268933900"

-- returns a random sound ID from the list (Like PaperThorn said)
local function getRandomSoundID(soundList)
    return soundList[math.random(1, #soundList)]
end

if DeathSound then
    DeathSound.SoundId = getRandomSoundID(DeathSoundIDs)  -- Randomly pick from the list
    DeathSound.Volume = 1.6
    DeathSound:Play() 
end

if JumpSound then
    JumpSound.SoundId = JumpSoundID
    JumpSound.Volume = 2.3
    JumpSound:Play()  
end

Ok so it works, however its playing the sound when you enter playtest and respawn, and its using the same one, how ever it did switch when I went into playtest again.
here is an example and what I am working with. sounds are temp until I can get something more fitting for what im going for

edit: while in game it did change

Well ofc it plays instantly, we aren’t telling it when to trigger, it’s just out there!

local npc = script.Parent 
local humanoid = npc:WaitForChild("Humanoid")
local deathSound = npc:WaitForChild("HumanoidRootPart"):WaitForChild("Died", 2)
local jumpSound = npc:WaitForChild("HumanoidRootPart"):WaitForChild("Jumping", 5)

local deathSoundIDs = {
    "rbxassetid://4596891584",  
    "rbxassetid://607648195"  
}

local jumpSoundID = "rbxassetid://268933900"  

local function getRandomSoundID(soundList)
    return soundList[math.random(1, #soundList)]
end

local function playDeathSound()
    if deathSound then
        deathSound.SoundId = getRandomSoundID(deathSoundIDs) 
        deathSound.Volume = 1.6
        deathSound:Play() 
    end
end

local function playJumpSound()
    if jumpSound then
        jumpSound.SoundId = jumpSoundID
        jumpSound.Volume = 2.3
        jumpSound:Play() 
    end
end

-- Event listener for NPC's death
humanoid.Died:Connect(function()
    playDeathSound()  
end)

-- Event listener for NPC jumping (Humanoid.Jumping)
humanoid.JumpingChanged:Connect(function(isJumping)
    if isJumping then
        playJumpSound() 
    end
end)

thanks a lot, I thought I would see what happened if I removed the “:Play()” parts from your first one and fixed the issue and its still working well other wise.

I was watching a video explaining tables and was slightly confused a bit then was in the process of copying the table from the actual roblox sound script and changing it hoping id get something to work.

this saved me the headache thank you! I’ll try and learn something from this as well

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.