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

How do I track deaths for my leaderboard script?

Hey I'm trying to make a leaderboard for deaths and my script doesn't really seem to track deaths and it's not showing any errors. I have two scripts, one for creating the leaderboard and one for tracking deaths. Here's my scripts, if possible could you explain what I did wrong? Thanks! Creating leaderboard script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local Deaths = Instance.new("IntValue",leaderstats)
	Deaths.Name = "Deaths"
	Deaths.Value = 0
	
end)

Tracking deaths script:

local function playerDead(player)
	local Deaths = player:WaitForChild("leaderstats").Deaths	
	local hum = player.Parent:FindFirstChild("Humanoid")
	if hum then
		if hum.Health == 0 then
			Deaths.Value = Deaths.Value + 1
			end
	end
end

game.Players.PlayerAdded:Connect(playerDead)

2 answers

✓ Accepted answer

Hello, AtrocityPrevails! There are quite a few problems with your script. First, you typed player.Parent:FindFirstChild("Humanoid"). This wouldn't work because the player's parent isn't its character. Instead, type player.Character. Second, the tacking deaths script would only work once because when the character respawns, the character that died would've gotten destroyed. Third, you used an if statement without a while loop. This wouldn't work because you wouldn't be constantly checking for the humanoid to die. Try this script:

game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			Deaths.Value = Deaths.Value + 1
		end)
	end)
end)

IMPROVEMENTS: I used :GetService(String Service) instead of the conditional dot operator. I also made it so that you don't use the second argument of Instance.new(string instance) since it's inefficient. Instead, manually set it by typing leaderstats.Parent = player, etc. The script above works because there's an event that fires when a character is added, and inside that event function, there is an event the fires when the player dies. When it does, it adds + 1 to the Deaths value. Hopefully, this helped you. Please accept my answer if it did.

7 comments

0

Ah you are 49 seconds faster. :)

0

Thank you so much for the help! Also, can you explain the GetService thing? When would you use that?

0

Oh, yeah. One of the good things about being a scripter is that you can type fast.

1

GetService("Players") is basically the same thing as game.Players. It's a good habit to use GetService() almost all the time since it checks if a service is existent. If it isn't it creates that service.

0

Wow, you're an amazing scripter! Thanks bro

0

No problem.

Instead of the current function you are using for player deaths, use this instead:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChild("Humanoid")
		Humanoid.Died:Connect(function()
			Player.leaderstats.Deaths.Value = Player.leaderstats.Deaths.Value + 1
		end)
	end)
end)

2 comments

0

Hey thanks for the help! I learned the 'Died' thing from you today :)

0

No problem!

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