My script work one time only

So this is the script I want it to repeat if the player die but the script only work if the player die in the first time but if he die again the script didn’t work

game.Players.PlayerAdded:Connect(function(player)
local Character = player.Character or player.CharacterAdded:Wait()
Character.Humanoid.Died:connect(function()
		local leaderstats = player:WaitForChild("leaderstats")
		local Dif = leaderstats:WaitForChild("Dif")	
		local Score = leaderstats:WaitForChild("Score")
		local BestScore = leaderstats:WaitForChild("BestScore")
		Dif.Value = 2
		player.PlayerGui.ScoreGui.ScoreButton.ScoreScriptServer.Disabled = true
		if Score.Value >= BestScore.Value then
			BestScore.Value = Score.Value
			Score.Value = 0
		else
			if Score.Value <= BestScore.Value then
				Score.Value = 0
	end
end
end)
end)
1 Like

Use CharacterAdded:Connect(function) after PlayerAdded
Here is a link Player | Roblox Creator Documentation

2 Likes
while true do
--your code
wait(0.01)--you may remove it if dont need wait 0.01 seconds or just change to yours
end
1 Like

I did not understade what you mean

no i want it to script to repeat one time if the player die

Use this code instead

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:connect(function()
		local leaderstats = player:WaitForChild("leaderstats")
		local Dif = leaderstats:WaitForChild("Dif")	
		local Score = leaderstats:WaitForChild("Score")
		local BestScore = leaderstats:WaitForChild("BestScore")
		Dif.Value = 2
		player.PlayerGui.ScoreGui.ScoreButton.ScoreScriptServer.Disabled = true
		if Score.Value >= BestScore.Value then
			BestScore.Value = Score.Value
			Score.Value = 0
		else
			if Score.Value <= BestScore.Value then
				Score.Value = 0
	end
end
end)
end)
end)
1 Like

do

game.Players.PlayerAdded:Connect(function(Player)
        Player.CharacterAdded:Connect(function(Char) -- this checks when the character respawns
               --- Your character died goes here
       End)
end)

still dont work

aaaaaaaaaaaaaaa

Like mentioned by @Sairx, you need to use put your died event in CharacterAdded.

This is because there is no way for the script to detect when the character loads again, so you need to do something like this

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:connect(function()
			-- stuff
		end)
	end)
end)

I tested it out on Studio and it runs every time your character dies.

@Dmitriy7628gamer the most efficient way to detect if a humanoid dies is the example I mentioned above. Loops are not useful in this case.

2 Likes

its still dont work note me script is normal script not local

well it should work, check output for any errors

You’re using the correct type of script, so everything should work well.

If you use my example, you can see how it prints “Died” every time your character dies. It might be a problem with the code you are putting in the humanoid died event.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:connect(function()
			print("Died")
		end)
	end)
end)

Tysm for your help thats work .

Ty all for you help .

aaaaaaaa

oh ok now i will know this thing :slight_smile:

You can mark mine or @Friendly4Crafter reply as a solution. So other people will find the solution easier if they have the same problem

1 Like