The code inside my humanoid.Died:Connect doesn't run

I’m trying to make a second chance gamepass for my obby game. How the gamepass should work is if you die with the gamepass, it will give you second chance to beat the obby.

However, the first time you die, it adds “UsedFirstChance” to your player and after that the whole :Connect() doesn’t run at all.

My code looked like this before I added the gamepass:

playerConnections[player.Name] = humanoid.Died:Connect(function()
	table.remove(players, i)
	player.Team = Teams.Lobby
	playerConnections[player.Name]:Disconnect()
	playerConnections[player.Name] = nil
	if findMapMusic(loadedMap.Name) ~= nil then
		MusicApi.stopMusic(player, findMapMusic(loadedMap.Name))
	end
	MusicApi.playLobbyMusic(player)
end)

But after I added the gamepass I changed the code to this:

playerConnections[player.Name] = humanoid.Died:Connect(function()
	if dataFolder:FindFirstChild("Gamepasses"):FindFirstChild("SecondChance").Value == true then
		if player:FindFirstChild("UsedFirstChance") ~= nil then
			table.remove(players, i)
			player.Team = Teams.Lobby
			playerConnections[player.Name]:Disconnect()
			playerConnections[player.Name] = nil
			if findMapMusic(loadedMap.Name) ~= nil then
				MusicApi.stopMusic(player, findMapMusic(loadedMap.Name))
			end
			MusicApi.playLobbyMusic(player)

			player:FindFirstChild("UsedFirstChance"):Destroy()
		else
			local usedFirstChanceValue = Instance.new("BoolValue")
			usedFirstChanceValue.Name = "UsedFirstChance"
			usedFirstChanceValue.Value = true
			usedFirstChanceValue.Parent = player
		end
	else
		table.remove(players, i)
		player.Team = Teams.Lobby
		playerConnections[player.Name]:Disconnect()
		playerConnections[player.Name] = nil
		if findMapMusic(loadedMap.Name) ~= nil then
			MusicApi.stopMusic(player, findMapMusic(loadedMap.Name))
		end
		MusicApi.playLobbyMusic(player)
	end
end)

I tried using a table instead of a BoolValue in your player but that didn’t work.

Thanks in advance!

1 Like

I doubt this solves your issues. But I was wondering why you use an object on the player instead of just keeping a table with player data in a module or something.