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.
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)
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.
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
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.
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.
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
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