Trying to make the audio keep playing after player dies

Oh, there’s actually an option on Sound Objects’s called Sound:Pause(). You can pause the music when they die, then move it to another location, move it back on the character when they respawn, then do Sound:Resume() and it will pick right back up where it left off.

if you move the Sound outside of the radio, there won’t be any need to assign the ID again.

I can move into the player and put it back?

the problem is that audio is inside the radio if I move again into the radio I will have 2 audios

Any place that you would like to temporarily store the Sound Object, then sure. The player object, maybe under a folder would be a good idea :+1:

Edit

You can do a very simple check to negate this issue. Instead of putting a Sound Object into the radio immediately, make it so if the user has played an audio, then don’t add another Sound Object on respawn. Something along the lines of this:

local PlayerHasPlayedAudio = false

if PlayerHasPlayedAudio == false then
    -- make a new Sound Object, and parent it to the players radio.
elseif PlayerHasPlayedAudio == true then
    -- look for the Sound Object under their Player object, and parent it back to the radio.
end

The format wouldn’t be this exact script! It’s just an example.

but I can remove that new audio?

Check the edit on my last reply.

Also I have school in about half an hour, but I’m going to try to quickly make a mockup for you.

Something like this mock-up “On Player Join Checker” that I made would do the trick for correctly issuing a new sound object, or fetching an already existing one.

Keep in mind, I named everything related to the Sound Objects name, “Sound”. Not anything else. You’ll have to adapt that if the Sound Objects name is something like “RadioSound”.

-- This is a mockup, not designed for your particular use case. Use this as a guide.

-- Get the Players Service --
local PlayersService = game:GetService("Players")

-- Hook the Join Function --
PlayersService.PlayerAdded:Connect(function(Player)
	
	-- Create a new folder to store anything. Like the sound object, and the bool.
	local NewAssetFolder = Instance.new("Folder")
	NewAssetFolder.Name = "AssetFolder"
	NewAssetFolder.Parent = Player
	
	-- Now create the bool object we can use to determine if the user did use the radio. This will only be created once. --
	local NewUserUsedRadioBool = Instance.new("BoolValue")
	NewUserUsedRadioBool.Name = "UserUsedRadio"
	NewUserUsedRadioBool.Parent = NewAssetFolder
	
	Player.CharacterAdded:Connect(function(Character)
		
		-- Get the location of the folder you stored the sound in on the Player Object --
		local AssetFolder = Player:WaitForChild("AssetFolder")
		local PlayerUsedRadioBool = AssetFolder:WaitForChild("UserUsedRadio")
		
		if PlayerUsedRadioBool.Value == true then
			-- Find the sound object, then move it back onto the radio, then play it again.
			local SoundObject = AssetFolder:WaitForChild("Sound")
			local RadioAccessory = Character:WaitForChild("Radio")
			
			SoundObject.Parent = RadioAccessory.Handle
			SoundObject:Resume()
		else
			-- This is for when the player is new to the game, or doesn't have any current music playing.
			local NewSoundObject = Instance.new("Sound")
			local RadioAccessory = Character:WaitForChild("Radio")
			
			NewSoundObject.Parent = RadioAccessory.Handle
		end
		
	end)
end)

Edit: Made a minor edit to the script, just under PlayersService.PlayerAdded:Connect(function(Player).

Edit 2: Oh yeah, forgot to mention that the UserUsedRadio BoolValue would be set when the player attempts to set their Sound Id through a GUI or something. When the server does that for them, make sure to tell the server to set the BoolValue to true, and false depending on if the SoundId field is empty or not, or if the Sound was stopped or not.

Edit 3: Just tested, and this does appear to work. Just make sure to move the Sound Object out of the characters radio when they die by using Humanoid.Died, and turn off the music by using Sound:Pause() so it saves the time position. Then when the player respawns, it will move the Sound back into their players radio, and resume it.

Video:

1 Like
-- This is a mockup, not designed for your particular use case. Use this as a guide.

-- Get the Players Service --
local PlayersService = game:GetService("Players")

-- Hook the Join Function --
PlayersService.PlayerAdded:Connect(function(Player)

	-- Create a new folder to store anything. Like the sound object, and the bool.
	local NewAssetFolder = Instance.new("Folder")
	NewAssetFolder.Name = "AssetFolder"
	NewAssetFolder.Parent = Player

	-- Now create the bool object we can use to determine if the user did use the radio. This will only be created once. --
	local NewUserUsedRadioBool = Instance.new("BoolValue")
	NewUserUsedRadioBool.Name = "UserUsedRadio"
	NewUserUsedRadioBool.Parent = NewAssetFolder

	Player.CharacterAdded:Connect(function(Character)

		-- Get the location of the folder you stored the sound in on the Player Object --
		local AssetFolder = Player:WaitForChild("AssetFolder")
		local PlayerUsedRadioBool = AssetFolder:WaitForChild("UserUsedRadio")

		if PlayerUsedRadioBool.Value == true then
			-- Find the sound object, then move it back onto the radio, then play it again.
			local SoundObject = AssetFolder:WaitForChild("Sound")
			local RadioAccessory = Character:WaitForChild("Radio")

			SoundObject.Parent = RadioAccessory.Handle
			SoundObject:Resume()
		else
			-- This is for when the player is new to the game, or doesn't have any current music playing.
			local NewSoundObject = Instance.new("Sound")
			local RadioAccessory = Character:WaitForChild("Radio")

			NewSoundObject.Parent = RadioAccessory.Handle
		
		local humanoid = Character:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function()
			RadioAccessory.Parent = AssetFolder
			end)
		end
	end)
end)

local MS = game:GetService("MarketplaceService")

local id = 21914253

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if MS:UserOwnsGamePassAsync(player.UserId,id) then
			local clone = game.ServerStorage.Radio:Clone()
			clone.Parent = character
		end
	end)
end)

game.ReplicatedStorage.RadioEvent.OnServerEvent:Connect(function(player,...)
	local tuple = {...}
	local arg1 = tuple[1]
	local arg2 = tuple[2]

	if arg1 == "PlaySong" then
		
		local character = player.Character
		local radio = character:FindFirstChild("Radio")
		
		if radio then
			radio:FindFirstChild("Handle").Sound.SoundId = "rbxassetid://" .. arg2
			radio:FindFirstChild("Handle").Sound:Play()
		end

	elseif arg1 == "StopSong" then
		local character = player.Character
		local radio = character:FindFirstChild("Radio")
		if radio then
			radio:FindFirstChild("Handle").Sound:Stop()
		end
	elseif arg1 == "GiveRadio" then
		local clone = game.ServerStorage.Radio:Clone()
		local character = player.Character
		clone.Parent = character
	end
end)

image
this is what happen the sound keep playing.

Why are you putting the radio in there? That folder was created for storing the sound object from the radio. Not the entire radio itself.

Is there a specific reason you’re trying to do it this way??

No I just dont understand how you did.

1 Like

As I mentioned before, I don’t have a full setup for this. In the attached video on my post, I used the command line to simulate what stuff would be changed if the player died. (Ex: moving sound to AssetFolder.)

All the RadioCheck script is for is checking if the user used the radio, and look for the sound so it can parent back to the radio accessory.

I moved the audio yes it moves but every time the player dies one audio get moved and I dont know how I can move it again likeI can do the humanoid.Died inside the function and change thePlayerUsedRadio to true?

Well, this entirely depends on how you give the radio to the player.

If you’re using a script to clone the radio accessory to the player if they have a gamepass, then you need to wrap this around that.

When setting the UserUsedRadio bool, it needs to be done on the server. You’re not setting it when they die. All Humanoid.Died purpose is, is to move the sound object to the AssetFolder, and to pause the music.

If the player is setting their music IDs through a GUI, you need to tell the server that the player has used the radio, by setting the bool there. After that, everything else clicks into place.

so basicly when the player clicks “Play” the PlayerUsedRadioBool will be true and if is true when the player dies the script will try to find the song and parent him on the handle?

Exactly! You don’t need to make a Sound object inside the players radio as the server automatically does that from the RadioCheck script, on every respawn, if they didn’t use it before.

I try to parent the audio into the folder and parent it again on radio I will reply after if it works.

1 Like

Any update on this? Just curious.

Sorry for the delay.

I am still trying to make the audio moves into the folder.

Try putting a script into the radio on the character, and make it find the humanoid. Once the humanoid is put into a variable, then make this:

Humanoid.Died:Connect(function()
-- make it so it moves the sound object to the folder under the player object
end)
1 Like