I’m making a “Points Adding” script. When a player kills someone, it will add 1 “kill” to that player. The player who is killed will get 1 “death”. But when I tested the game, I killed a player, it added 12 “kills” for me and the player who was killed got 12 deaths instead of 1.
Here’s the “leaderstats” script:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Value = 0
kills.Name = "Kills"
kills.Parent = leaderstats
local deaths = Instance.new("IntValue")
deaths.Value = 0
deaths.Name = "Deaths"
deaths.Parent = leaderstats
end)
The “DamageCharacter” script:
local function damageCharacter(playerFired, otherPart)
local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
local victim
if humanoid then
if not isTouched then
isTouched = true
humanoid:TakeDamage(damage)
victim = Players:GetPlayerFromCharacter(humanoid.Parent)
humanoid.Died:Connect(function()
remoteEventsFolder.GiveKill:FireAllClients(playerFired, victim)
remoteEventsFolder.DamageCharacter:FireAllClients(playerFired, victim)
end)
wait(1)
isTouched = false
end
end
end
The “PointReceiver” script:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEventsFolder = ReplicatedStorage:WaitForChild("RemoteEventsFolder")
local playerLeaderstats = player:WaitForChild("leaderstats")
local playerKills = playerLeaderstats:FindFirstChild("Kills")
local playerDeaths = playerLeaderstats:FindFirstChild("Deaths")
remoteEventsFolder.GiveKill.OnClientEvent:Connect(function(playerFired, victim)
if player == playerFired then
playerKills.Value = playerKills.Value + 1
elseif player == victim then
playerDeaths.Value = playerDeaths.Value + 1
else
playerKills.Value = playerKills.Value + 0
playerDeaths.Value = playerDeaths.Value + 0
end
end)
I think it is because you didn’t disconnect the “humanoid.Died” function so on the 12th hit, that function would fire 12 times giving you 12 kills and the other player 12 deaths, maybe. Also, I usually set variables create in a table to nil after all the commands ran.
From my experience, if an event is put in a function, it will be created every time that function is called so the died event will still fire again (if the function is called again and “isTouched” is false). Also, adding a debounce in the “humanoid.Died” function may not help since all the functions created will still fire when debounce is false (too complicated to explain :)).
local canAdd = false
remoteEventsFolder.GiveKill.OnClientEvent:Connect(function(playerFired, victim)
if player == playerFired then
if not canAdd then
canAdd = true
playerKills.Value = playerKills.Value + 1
playerKills.Changed:Wait()
canAdd = false
end
elseif player == victim then
if not canAdd then
canAdd = true
playerDeaths.Value = playerDeaths.Value + 1
playerDeaths.Changed:Wait()
canAdd = false
end
else
playerKills.Value = playerKills.Value + 0
playerDeaths.Value = playerDeaths.Value + 0
end
end)
It added 1 kill to the killer and 1 death to the victim. But the problem is: this will only add “kills” and “deaths” to the player once. When I killed the player the second time, it didn’t add any “kills” for me and any “deaths” for the victim.
I think it is because the “playerKills.Changed:Wait()” and the “playerDeaths.Changed:Wait()”, you should remove that, since “playerKills” and “playerDeaths” have already changed, therefore, waiting for them to change after they’ve changed would be useless, maybe. If you still want to use them do like this:
Actually I realized there is no need to use Humanoid.Died event
just check if humanoid.Health <= 0, then give points
also no need to :FireAllClients to give points because it should be handle on server script
when player kill other player