Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to know how many times a player dies?

local player = game:GetService(“Players”)

for _, v in pairs (player:GetPlayers()) do
   local deathCounter = Instance.new(“IntValue”)
   deathCounter.Name = “DeathCounter”
   deathCounter.Parent = v
end

what i need to happen is when a player dies, their deathCounter value should go up by one. How do i make this happen? Also should this be in a local script or server script? Thanks!

1 answer

✓ Accepted answer

This would need to be in a server script if you want everyone to see the deaths. As for detecting when a player dies, you would need to make use of the Died function for the player's Humanoid.

Code:

game.Players.PlayerAdded:Connect(function(player)
	local deathCounter = Instance.new("IntValue",player)
		  deathCounter.Name = "DeathCounter"
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			deathCounter.Value = deathCounter.Value + 1
			print(player.Name.." has died "..deathCounter.Value.." time(s)")
		end)
	end)
end)

Insert this script inside ServerScriptService

3 comments

0

um, I think you forget to put the deathCounter’s parent

1

He set the parent of deathCounter on line 2. Instance.new() has an second argument which is the parent of that Instance, so on line 2, he's creating the 'IntValue' and the second argument is the parent of that Instance, so he's setting the IntValue's parent to the player.

0

ah yes sorry. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.