Can someone please explain to me why the kills are not working on this leaderstats script? Thanks!
local Players = game.Players local Template = Instance.new 'BoolValue' Template.Name = 'leaderstats' Instance.new('IntValue', Template).Name = "Kills" Instance.new('IntValue', Template).Name = "Deaths" Players.PlayerAdded:connect(function(Player) wait(1) local Stats = Template:Clone() Stats.Parent = Player local Deaths = Stats.Deaths Player.CharacterAdded:connect(function(Character) Deaths.Value = Deaths.Value + 1 local Humanoid = Character:FindFirstChild "Humanoid" if Humanoid then Humanoid.Died:connect(function() for i, Child in pairs(Humanoid:GetChildren()) do if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then local Killer = Child.Value if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then local Kills = Killer.leaderstats.Kills Kills.Value = Kills.Value + 1 end return end end end) end end) end)
I see. Player.CharacterAdded
won't fire when a player joins or the Players.PlayerAdded
event. To fix this, you create a Script
inside StarterCharacterScripts
.
Why put in StarterCharacterScripts? It's because everything inside StarterCharacterScripts
will be moved to the player's character once it loaded/respawned. Not only it will be moved, but making a Script
a descendant of Workspace
(Player.Character
is a descendant of Workspace
too) will automatically start running the script.
After creating the Script
, copy this code:
local Players = game:GetService("Players") local Character = script.Parent local Humanoid = Character:FindFirstChildOfClass("Humanoid") local Player = Players:GetPlayerFromCharacter(Character) local Stats: Folder = Player:FindFirstChild("leaderstats") local Kills: IntValue = Stats:FindFirstChild("Kills") local Deaths: IntValue = Stats:FindFirstChild("Deaths") Humanoid.Died:connect(function() for _, Child in pairs(Humanoid:GetChildren()) do local Killer = Child.Value if Child:IsA("ObjectValue") and Killer and Killer:IsA("Player") then local OtherStats = Killer:FindFirstChild("leaderstats") local OtherKills = OtherStats:FindFirstChild("Kills") if OtherStats and OtherStats:IsA("Folder") and OtherKills and OtherKills:IsA("IntValue") then Deaths.Value += 1 OtherKills.Value += 1 end end end end)
Make another Script
inside ServerScriptService
and this is where we make the leaderstats.
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(Player) local Stats = Instance.new("Folder", Player) Stats.Name = "leaderstats" local Kills = Instance.new("IntValue", Stats) Kills.Name = "Kills" local Deaths = Instance.new("IntValue", Stats) Deaths.Name = "Deaths" end)
After doing all that, you may now delete your old leaderstats script.