Kills and Deaths script problem

So I have a Kills and Deaths leaderstats script but I don’t want the Deaths to count when I change teams with my team changer, for my team changer I use “LoadCharacter”, and when I change teams it give +1 to Deaths leaderstats and that’s what I don’t want. If anyone knows how to do it I would really appreciate it! Thank you in advance!

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)

1 Like

why don’t you script “Deaths = Deaths + 1” Under “ if Child:IsA(‘ObjectValue’) and Child.Value and Child.Value:IsA(‘Player’) then” .
In that case.Only someone killed by another.Deaths will increase

1 Like

Just as @xiaos_j said, you should move the Deaths counter to be under the if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then part of your code.

By using :LoadCharacter(), the connection Player.CharacterAdded:Connect(function(Character) also runs, behaving in the exact same way as a death. Moving this part Deaths.Value = Deaths.Value + 1 to be right before you assign a kill to the killer would cause deaths to only count upon being killed, not just having your character reset.

It would look something like this:

Player.CharacterAdded:Connect(function(Character)
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	
	if Humanoid then
		Humanoid.Died:Connect(function()
			for _,Child in pairs(Humanoid:GetChildren()) do
				if not Child:IsA("ObjectValue") then continue end ;
				
				if Child.Value and Child.Value:IsA("Player") then
					Deaths.Value += 1
					
					local Killer = Child.Value
					
					if Killer:FindFirstChild("leaderstats") and Killer.leaderstats:FindFirstChild("Kills") then
						local Kills = Killer.leaderstats.Kills
						Kills.Value += 1
					end
					
					return
				end
			end
		end)
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.