Player Respawns And Sound Plays Again

How to make it so that when a player has already touched a checkpoint and the sound is played, if they end up dying and respawns, the sound would not be played again??

Every time I reset my character while testing and I respawn on a checkpoint that I have already touched and the sound has already played, the sound plays again.

Here’s the script:

local CKsound = workspace.SoundLibrary.CheckpointSound
local Players = game:GetService("Players")
local CharSpawn = {}


Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Char)
		Char = CharSpawn
		local PlayerLevel = player:WaitForChild("leaderstats").Level
		wait(PlayerLevel)
		local play = false
		local  debounce = false
		local LevelName = script.Parent.Parent.Parent.Parent.Checkpoints["2"]
		print("Online")

		if not CharSpawn[player.Name] and LevelName.Name ~= PlayerLevel.Value then
			CharSpawn[player.Name] = true
			print(player.Name, "has spawned on a different checkpoint")
		end


		script.Parent.Touched:Connect(function(hit)
			for i, parts in ipairs(script.Parent:GetTouchingParts()) do
				if parts == hit.Parent or not hit.Parent:FindFirstChildWhichIsA("Humanoid") then
					debounce = false
					print("Touch reset")
				else	
					if hit.Parent:FindFirstChildWhichIsA("Humanoid") and parts ~= hit.Parent then
						local Player = Players:GetPlayerFromCharacter(hit.Parent)
						if Player and debounce == false then
							debounce = true
							print("ACTIVE")


							if Player and play == false then	
								if LevelName ~= nil then			
									print("Part Name is", LevelName, "and Player Level is ", PlayerLevel.Value)


									if not CharSpawn[player.Name] and LevelName.Name == PlayerLevel.Value then
										play = true													
										print(player.Name, "has spawned on same checkpoint")
									end


									if CharSpawn[player.Name] == true and LevelName.Name == PlayerLevel.Value then
										play = true
										CKsound:Play()		
										print("Checkpoint reached")
										print(Player, "has touched Checkpoint", LevelName.Name)
									else
										if CharSpawn[player.Name] == true and LevelName.Name ~= PlayerLevel.Value then	
											play = false	
											print("Checkpoint not reached")
										end
									end


									for i, char in ipairs(Players:GetPlayers()) do	
										local humanoid = char.Character:WaitForChild("Humanoid")
										humanoid.Died:Connect(function()
											if play == true and LevelName.Name == PlayerLevel.Value then
												print(player.Name, "has died!")
												CKsound:Stop()	
												print("Checkpoint touched already")
											end
										end)
									end


									PlayerLevel:GetPropertyChangedSignal("Value"):Connect(function()
										print("Part Name is", LevelName, "and Player Level is ", PlayerLevel.Value)	
										if LevelName.Name == PlayerLevel.Value then
											play = true
											CKsound:Play()		
											print("Checkpoint reached")
											print(Player, "has touched Checkpoint", LevelName.Name)
										else
											if LevelName.Name ~= PlayerLevel.Value then	
												play = false		
												print("Checkpoint not reached")	
											end												
										end											
									end)											
								end										
							end								
						end
					end					
				end	
			end	
		end)
	end)
end)


Players.PlayerRemoving:Connect(function(plr)
	table.remove(CharSpawn, table.find(CharSpawn, plr.Name))
	print(plr, "left")
end)

This piece of coding that I added to help fix the issue is not helping either:

for i, char in ipairs(Players:GetPlayers()) do	
										local humanoid = char.Character:WaitForChild("Humanoid")
										humanoid.Died:Connect(function()
											if play == true and LevelName.Name == PlayerLevel.Value then
												print(player.Name, "has died!")
												CKsound:Stop()	
												print("Checkpoint touched already")
											end
										end)
									end

It’s because you aren’t disconnecting any of your events. They are duplicating themselves each time you respawn.

I tried but it still has the same outcome.

if you want to check whenever player’s character gets added you can use CharacterAdded event

1 Like

make a variable so when you touched the respawn part it will become true. When you touch the respawn part, it will check if the variable is true , if not it will play a sound then make the variable true. I’m so lazy to script :smiley:

1 Like

On touching the brick, add player into a table:

local TouchedPlayers = {}
game.Workspace.Brick.Touched:Connect(function(hit) --whatever your part is
     if hit.Parent:FindFirstChild("Humanoid") then
          if not table.find(TouchedPlayers, hit.Parent.Name) then -- if player isn't in table already then runs code below
                table.insert(TouchedPlayers, hit.Parent.Name) -- inserts player in table so the sound wont run again
                Sound:Play()
          end
     end
end)
Players.PlayerRemoving:Connect(function(player) -- removes player from table if the player leaves game 
     if table.find(TouchedPlayers, player.Name) then -- if player is in the table then runs the code below
           local pos = table.find(TouchedPlayers, player.Name)
           table.remove(TouchedPlayers, pos) -- removes player from table
     end
end)

This block of code will allow you to make it so that the sound isn’t played anymore even if the player touches the part

2 Likes

i think its because of this line
you’re checking if the LevelName is the same as the PlayerLevel
you have to do if LevelName.Name >= PlayerLevel.Value then

1 Like

Used this to help me in a way but I made a spin to it. Basically the HealthChanged event solved the code:

	for i, char in pairs(PlayerTable) do	
			if char.Character:WaitForChild("Humanoid") then
				local humanoid = char.Character.Humanoid
				humanoid.HealthChanged:Connect(function()
					if humanoid.Health == 0 then
						death = humanoid.Died:Connect(function()
							print(player.Name, "has died!")
						end)
					end
				end)
			end
		end
1 Like

Revamped the code using this and it works so efficiently, thanks man!

1 Like