Music Restarts When Player Respawns

hello! i’ve a got a script for different sound regions, and everything works fine except that when the player dies and respawns, the music in that region will restart instead of continuing.

heres my local script in startercharacterscripts:

local AreaSounds = Instance.new("Sound", script)
AreaSounds.Looped = true
local plr = script.Parent
local Presser = plr:WaitForChild("HumanoidRootPart")
local DifferentAreas = workspace:WaitForChild("AreaSounds")
local CurrentArea = nil

game:GetService("RunService").Heartbeat:Connect(function()
	local raycast = Ray.new(Presser.Position, Presser.CFrame.UpVector * -1000)
	local part = workspace:FindPartOnRayWithWhitelist(raycast, {DifferentAreas})
	if part and part.Parent == DifferentAreas then
		if part ~= CurrentArea then
			CurrentArea = part
			AreaSounds.SoundId = CurrentArea.Sound.SoundId
			AreaSounds:Play()
		end 
	else
		CurrentArea = nil
		AreaSounds:Stop()
	end
end)

any help would be GREATLY appreciated! thanks! :))

1 Like

Put it in StarterPLAYERScripts. If you put it into character scripts, that just deletes the script every time the player’s character dies and makes a new one when they respawn.

i did that, and the script stopped working entirely.

Hello! It seems like the issue you’re facing is that the music in the sound region restarts when the player dies and respawns. To resolve this, you can modify your script to store the current playing time of the “AreaSounds” sound and resume playback from that point when the player respawns. Here’s an updated version of your script with the necessary modifications:

local AreaSounds = Instance.new("Sound", script)
AreaSounds.Looped = true
local plr = script.Parent
local Presser = plr:WaitForChild("HumanoidRootPart")
local DifferentAreas = workspace:WaitForChild("AreaSounds")
local CurrentArea = nil
local soundPlaybackTime = 0 -- Store the current playing time of the sound

game:GetService("RunService").Heartbeat:Connect(function()
	local raycast = Ray.new(Presser.Position, Presser.CFrame.UpVector * -1000)
	local part = workspace:FindPartOnRayWithWhitelist(raycast, {DifferentAreas})
	if part and part.Parent == DifferentAreas then
		if part ~= CurrentArea then
			CurrentArea = part
			AreaSounds.SoundId = CurrentArea.Sound.SoundId

			-- Set the playback time to the stored value
			AreaSounds.TimePosition = soundPlaybackTime

			AreaSounds:Play()
		end 
	else
		CurrentArea = nil
		AreaSounds:Stop()
	end
end)

-- Store the current playing time of the sound when the player dies
plr.CharacterRemoving:Connect(function()
	soundPlaybackTime = AreaSounds.TimePosition
end)
1 Like

Why not just make the music play on the server side?

Lets say one player enters an underwater area, you wouldn’t want everyone on the server to hear music/sounds that are from that area.

Make the audio hearable only in that area?

sorry for the late response, but after a few adjustments, it worked! thanks!

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