Health Change Response Issue

  1. What do you want to achieve?
    I’m currently making a simple Survive from Killer game (Survive from Tankman). I’m trying to make a Leaderboard for Deaths and Seconds Alive.

  2. What is the issue?
    So apparently, I made a server sided script, that makes a Leaderboard of Deaths and Seconds Alive, and adds a value (1) for in Deaths and resets the Seconds Alive counter (Which I’ll be doing later), but it seems it’s not working. Here’s the script:

local Players = game:GetService("Players")

-- Leaderboard Configuration --
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"

-- Data Configuration --
local SecondsAlive = Instance.new("IntValue")
SecondsAlive.Name = "Seconds Alive"
SecondsAlive.Parent = leaderstats

local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats

-- Leaderboard Setup --
Players.PlayerAdded:Connect(function(Player)
	
	leaderstats.Parent = Player
end)

-- SecondsAlive Setup --
while true do
	
	SecondsAlive.Value = SecondsAlive.Value + 1
	wait(1)
end

-- Response On Death --
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		local Humanoid = Character.Humanoid
		Humanoid.HealthChanged:Connect(function(Health)
			
			if Health == 0 then
				
				Deaths.Value = Deaths.Value + 1
			end
		end)
	end)
end)
  1. What solutions have you tried so far?
    Before, it was a LocalScript, but it wasn’t working. So I tried using HealthChanged function (As shown), but it doesn’t work either.

All error notices are accepted, thank you!

Try to use Humanoid.Died event

Humanoid.Died:Connect(function()
	Deaths.Value = Deaths.Value + 1
end)
1 Like

It still doesn’t work, but thanks for sharing!

Humanoid.Died Event doesn’t work, does it only work on LocalScripts? I will give that a shot in a moment.

Thank you!

Mmm… If that events don’t fire on LocalScripts, try to use Remove Events

1 Like

It doesn’t work on LocalEvents.

wait, could you show me the explorer tab?

There’s a few problems with what you’re currently doing. With your current code, everyone uses the same leaderstats folder so players don’t have their own individual stats. To fix this, you clone the leaderstats folder and then parent it to the player, so that everyone has their own stats. The other problems are with how you’re getting the stats. Instead of getting each player’s individual stats, you were still referencing the single folder.

With all of this in mind, I made the necessary edits and put some comments to help you understand. Let me know if you have any questions.

local Players = game:GetService("Players")

-- Leaderboard Configuration --
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"

-- Data Configuration --
local SecondsAlive = Instance.new("IntValue")
SecondsAlive.Name = "Seconds Alive"
SecondsAlive.Parent = leaderstats

local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats

-- Leaderboard Setup --
Players.PlayerAdded:Connect(function(Player) -- Removed the other PlayerAdded event, no reason to have two
	leaderstats:Clone().Parent = Player
	
	local function OnCharacterAdded(character)
		local Humanoid = character:WaitForChild("Humanoid") -- WaitForChild() in case humanoid doesn't initially exist
		Humanoid.Died:Connect(function(Health) -- Replaced HealthChanged with Died
			Player.leaderstats.Deaths.Value += 1
		end)
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
	if Player.Character then -- if the player's character already exists before the script loads
		OnCharacterAdded(Player.Character)
	end
end)

-- SecondsAlive Setup --
while true do
	for _, player in pairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local timeAliveValue = leaderstats:FindFirstChild("Seconds Alive")
			if timeAliveValue then
				timeAliveValue.Value += 1
			end
		end
	end
	
	wait(1)
end

1 Like

The events work fine both on the server and locally, the problem was the logic in your code. No remotes are necessary for what you’re trying to do. The code should be in a server script in ServerScriptService.

1 Like

What does += mean? I thought it was only used for parsing conditions.

+= is just syntactic sugar added by Luau. It’s the same as doing Player.leaderstats.Death.Value = Player.leaderstats.Death.Value + 1.

1 Like

Thank you so much! I’m still new to scripting, but this solution worked!

Now this time, I was trying to do something, remember the SecondsAlive Setup? If the player dies, the SecondsAlive resets, how do I do that?

Thanks for solving this problem, you don’t need to solve this if you wish. I will just turn the SecondsAlive to ServerTime, thank you!

To have the Seconds Alive value reset once they die, just add this line after line 23. Player.leaderstats["Seconds Alive"].Value = 0. However, if you would prefer to wait until they’ve respawned so no seconds are given for sitting there, you should put that line of code after the local function OnCharacterAdded(character) line.

1 Like

Like this?

local function OnCharacterAdded(character)
		local Humanoid = character:WaitForChild("Humanoid") -- WaitForChild() in case humanoid doesn't initially exist
		Humanoid.Died:Connect(function(Health) -- Replaced HealthChanged with Died
			Player.leaderstats.Deaths.Value += 1
			
			local function OnCharacterAdded(character)
				Player.leaderstats["Seconds Alive"].Value = 0
			end
		end

Script: Line 20 - 28

I tried running it and it didn’t work.

Almost. You don’t need to copy the OnCharacterAdded() function, but the line of code I gave you should be in the original CharacterAdded function.

local function OnCharacterAdded(character)
	Player.leaderstats["Seconds Alive"].Value = 0 -- Resets when the character respawns
	local Humanoid = character:WaitForChild("Humanoid") -- WaitForChild() in case humanoid doesn't initially exist
	Humanoid.Died:Connect(function(Health) -- Replaced HealthChanged with Died
		Player.leaderstats.Deaths.Value += 1
	end)
end
1 Like

Hello, it seems that it finally works now, thank you!

I will surely credit you once the game gets published. Along with other people who given out solutions too.

Have a great day! :smiley:

1 Like