Local script resetting on death

I have local script that plays music, it works fine. But when the player dies the music resets and picks another song. I couldn’t find any online sources that would contribute into helping me fix, so I went to devforum.

local Music = script:WaitForChild('Music')
local Songs = Music:GetChildren()

local LastPlayed

local function PlayRandomSong()
	local song = Songs[math.random(1,#Songs)]
	if not song.Playing and song.Name ~= LastPlayed then
		song:Play()
		song.Ended:Wait()
		LastPlayed=song.Name
		PlayRandomSong()
	elseif song.Name == LastPlayed then
		PlayRandomSong()
	end
end
PlayRandomSong()

Remove it from the starterGui and put it in starterplayerscripts

1 Like

its because the script is being remade every time you die.
Put your script somewhere where it won’t be recreated on death.
You can put it in StarterPlayer > StarterPlayerScripts

Thank you so much, it was common knowledge that I had forgotten.

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