Hello i wanted to create kills and deaths leaderboard system it works but when player kills someone he gets +2 kills instead of 1
This is my code:
game.Players.PlayerAdded:Connect(function(plr)
local a = Instance.new("Folder")
a.Parent = plr
a.Name = "leaderstats"
local b = Instance.new("IntValue")
b.Parent = a
b.Name = "Kills"
local c = Instance.new("IntValue")
c.Parent = a
c.Name = "Deaths"
plr.CharacterAdded:Connect(function(char)
local hum = char.Humanoid or char:WaitForChild("Humanoid")
hum.Died:Connect(function()
local killer = hum:FindFirstChild("creator")
plr.leaderstats.Deaths.Value += 1
game:GetService("Players"):FindFirstChild(killer.Value.Name).leaderstats.Kills.Value += 1
end)
end)
end)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local a = Instance.new("Folder")
a.Parent = plr
a.Name = "leaderstats"
local b = Instance.new("IntValue")
b.Parent = a
b.Name = "Kills"
local c = Instance.new("IntValue")
c.Parent = a
c.Name = "Deaths"
plr.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local hum = character.Humanoid:FindFirstChild("creator")
if hum ~= nil then
if hum.Value ~= nil then
local killer = hum:FindFirstChild("creator")
plr.leaderstats.Deaths.Value += 1
Players:FindFirstChild(killer.Value.Name).leaderstats.Kills.Value += 1
end
end
end)
end)
end)
There isn’t a need to index into the leader stats folder to add/subtract values from it. Because you have the variables in the same scope that your trying to do arithmetic operations on, you can instead directly access the variables and modify their values that way.
local c = Instance.new("IntValue")
c.Parent = a
c.Name = "Deaths"
-- because kills is in the same scope, we can access them
c.Value += 1
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local a = Instance.new("Folder")
a.Parent = plr
a.Name = "leaderstats"
local b = Instance.new("IntValue")
b.Parent = a
b.Name = "Kills"
local c = Instance.new("IntValue")
c.Parent = a
c.Name = "Deaths"
plr.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
local killer = hum:FindFirstChild("creator")
plr:WaitForChild("leaderstats"):WaitForChild("Deaths").Value += 1
Players:FindFirstChild(killer.Value.Name):WaitForChild("leaderstats"):WaitForChild("Kills").Value += 1
end)
end)
end)
That was a typo, but it’s still recommended you access the values directly via through the variable rather than searching for them in leader stats folder.