An issue with leaderstats kills

So i tried making a kills and deaths leaderboard , but the problem is whenever i try to kill the enemy, they get 1 kill and 1 death.
Any ideas of what the problem is?

The Code:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderboard = Instance.new("Folder",player)
	leaderboard.Name = "leaderstats"
	
	local kills = Instance.new("IntValue",leaderboard)
	kills.Name = "Kills"
	kills.Value = 0
	
	local deaths = Instance.new("IntValue",leaderboard)
	deaths.Name = "Deaths"
	deaths.Value = 0
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		local Tagged = Instance.new("ObjectValue",Humanoid)
		Tagged.Name = "creator"
		Tagged.Value = player
		Humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local Tag = Humanoid:FindFirstChild("creator")
			local killer = Tag.Value
			if Tag and killer then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)
end)

Thanks in advance!

What does the variable killer equal? You could try game.Players[killer] if killer is a string value.

tbh i tried making a leaderstats on my own, but i didnt know how to make kills and deaths one, so i searched the youtube and saw a video, this was his script except the tagged variable which i got from people explaining what the ‘creator’ is. if you can give me a brief explaination of what the creator tag is , that will be much appreciated. anyways his code only had

local tag = Humanoid:FindFirstChild("creator") 

but that gave me a nil value (error)
so if you can help me with this code this will be much appreciated!

Does the weapon that you use actually create a creator tag? If so, try waiting for the creator tag to spawn because it might run before it makes the tag.

The script that have the leaderstats code is in a different script
i tried waiting but that didnt work
the death event works perfectly but the kills doesnt
this is my first time using the creator tag so i dont know much about it

your not properly checking if he is a killer

local killer = Tag.Value
if Tag and killer then

your only checking if his tag has a value, it should be something this:

local killer = Tag.Value
if Tag and killer == 'Killer' then
1 Like

So the Code should be like this?

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local leaderboard = Instance.new("Folder",player)
	leaderboard.Name = "leaderstats"
	
	local kills = Instance.new("IntValue",leaderboard)
	kills.Name = "Kills"
	kills.Value = 0
	
	local deaths = Instance.new("IntValue",leaderboard)
	deaths.Name = "Deaths"
	deaths.Value = 0
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		local Tagged = Instance.new("ObjectValue",Humanoid)
		Tagged.Name = "creator"
		Tagged.Value = player
		Humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local Tag = Humanoid:FindFirstChild("creator")
			local killer = Tag.Value
			if Tag and killer == 'killer' then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)
end)

i tried this but didnt work (i removed the ‘’ from the killer but this didnt work too)

Can you show us your weapon’s script?

ok sure but that will take alot of space
there are 2 scripts: local script and a server script

server script:

--Main
local Tool = script.Parent
local Blade = Tool:WaitForChild("Blade")
local AnimationRemote = Tool:WaitForChild("Remote")
local MyRoot
--Variables
local Event_
local state = nil
local idleAnim1
local idleAnim2

local ComboCount = 1 --FirstSwing
local LastSwing = tick()
local Deb = {}
local Debounce = true
local CanDamage = false

local Damage = 6

--Animations
local idle = Tool.Handle.Idle
local run = Tool.Handle.Running


--Sounds
local slash1hit = script.Parent.Handle.HitSound1
local slash2hit = script.Parent.Handle.HitSound2
local slash3hit = script.Parent.Handle.HitSound3
local slash1miss = script.Parent.Handle.MissSound1
local slash2miss = script.Parent.Handle.MissSound2
local slash3miss = script.Parent.Handle.MissSound3



--Functions
Blade.Touched:Connect(function(hit)
	local MyHuman = Tool.Parent:FindFirstChildWhichIsA("Humanoid")
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if CanDamage and not Deb[Humanoid] and not hit.Parent:IsDescendantOf(Tool.Parent) and Humanoid then
		
		Deb[Humanoid] = true
		--HitSounds
		print(ComboCount .. " is the combo count")
		if Humanoid then
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - (Damage * ComboCount)
			end
		if ComboCount == 1 then
			slash1hit:Play()
			
		end
		if ComboCount == 2 then
			slash2hit:Play()
			
		end
		if ComboCount == 3 then
			slash1hit:Play()
		end
		if ComboCount == 4 then
			slash3hit:Play()
		end
		CanDamage = false
	end
end)


Tool.Equipped:Connect(function()
	MyRoot = Tool.Parent:FindFirstChild("HumanoidRootPart")
	local humanoid = Tool.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then

		idleAnim1 = humanoid:LoadAnimation(idle)
		idleAnim1:Play()

		--// Check for movement
		Event_ = humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()

			local magnitude = humanoid.MoveDirection.Magnitude

			if magnitude <= 0.1 and state ~= "idle" then
				state = "idle"
				idleAnim1 = humanoid:LoadAnimation(idle)
				idleAnim1:Play()
				
				if idleAnim2 then
					idleAnim2:Stop()
				end

			elseif state ~= "other" then
				state = "other"
				idleAnim2 = humanoid:LoadAnimation(run)
				idleAnim2:Play()
				if idleAnim1 then
					idleAnim1:Stop()
				end
			end
		end)
	end
end)
	--if idleAnim1 then
	--	idleAnim1:Stop()
	--end


	--else		
	--print("No Humanoid Detected")		
	--end
	--end)
--end)



Tool.Activated:Connect(function()
	local humanoid = Tool.Parent:FindFirstChildWhichIsA("Humanoid")
	local playanim = humanoid:LoadAnimation(idle)
	if Debounce then
		Debounce = false
		CanDamage = true
		Deb = {}
		local ExtraDelay = 0
		if tick()-LastSwing > 4 or ComboCount >= 4 then
			--reset
			ComboCount = 1
			if ComboCount >= 3 then
				ExtraDelay = 2
			end
		else
			--increase combo
			ComboCount = ComboCount + 1
			if ComboCount >= 4 then
				ExtraDelay = 2
			end	
		end
		if ComboCount == 1 then
			slash1miss:Play()
		end
		
		if ComboCount == 2 then
			slash2miss:Play()
			
		end
		if ComboCount == 3 then
			slash1miss:Play()
			
		end
		if ComboCount == 4 then
			slash3miss:Play()
		end
		playanim:Stop()
		AnimationRemote:FireAllClients(ComboCount)
		LastSwing = tick()
		
		wait(.3)
		CanDamage = false
		
		wait(ExtraDelay)
		Debounce = true
	end
end)


Tool.Unequipped:Connect(function()
	Event_:Disconnect()
	if idleAnim1 then idleAnim1:Stop() end
	if idleAnim2 then idleAnim2:Stop() end
end)

local script:

local Tool = script.Parent
local remote = Tool:WaitForChild("Remote")

local AnimationIDs = {
	"rbxassetid://8558593525",
	"rbxassetid://8566693750",
	"rbxassetid://8566839020",
	"rbxassetid://8566868710"
}

local LoadedAnimation = {}
local tInsert = table.insert

local ToolEquipped = false

---------------------------------------
remote:FireServer()


Tool.Equipped:Connect(function()
	ToolEquipped = true
	local Humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid then
		LoadedAnimation = {}
		for i = 1, #AnimationIDs do
			local AnimationObject = Instance.new("Animation")
			AnimationObject.AnimationId = AnimationIDs[i]
			tInsert(LoadedAnimation, Humanoid:LoadAnimation(AnimationObject))
		end
	end
end)



remote.OnClientEvent:Connect(function(Number)
	-- play animation it goes with
	if #LoadedAnimation > 0 and LoadedAnimation[Number] then
		LoadedAnimation[Number]:Play()
	end
end)

also there is a remote event called “remote”

Where do you actually make the Creator tag? I can’t see that anywhere in your script. If you are going to add it make it server side.

the sword script and the leaderstats script are different scripts
should i combine them into one script?

I don’t think so, all you need to do is where you damage the player add this:

if Humanoid then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - (Damage * ComboCount)
    if hit.Parent.Humanoid.Health < 1 then
        local tag = Instance.new("StringValue")
        tag.Name = "creator"
        tag.Value = MyHuman.Parent.Name
    end
end

Something like that should work.

there is an error code now
its

15:29:29.461  ServerScriptService.LeaderBoard:24: attempt to index nil with 'FindFirstChild'  -  Server - LeaderBoard:24

in this code

also one more thing
when i delete this code which is a

		local Tagged = Instance.new("ObjectValue",Humanoid)
		Tagged.Name = "creator"
		Tagged.Value = player

it gives me that error

but if i keep it, 1 kill and 1 death goes to the one i kill
he gets 1 death and thats good.
but the kill should go to me.
also thank you for sticking with me i’m still a beginner.

Firstly thanks for everyone who tried to help
i figured it out
here are the scripts if anyone faces the same problem

SwordScript:

--Main
local Tool = script.Parent
local Blade = Tool:WaitForChild("Blade")
local AnimationRemote = Tool:WaitForChild("Remote")
local MyRoot
--Variables
local Event_
local state = nil
local idleAnim1
local idleAnim2

local ComboCount = 1 --FirstSwing
local LastSwing = tick()
local Deb = {}
local Debounce = true
local CanDamage = false

local Damage = 50

--Animations
local idle = Tool.Handle.Idle
local run = Tool.Handle.Running


--Sounds
local slash1hit = script.Parent.Handle.HitSound1
local slash2hit = script.Parent.Handle.HitSound2
local slash3hit = script.Parent.Handle.HitSound3
local slash1miss = script.Parent.Handle.MissSound1
local slash2miss = script.Parent.Handle.MissSound2
local slash3miss = script.Parent.Handle.MissSound3



--Functions
Blade.Touched:Connect(function(hit)
	local MyHuman = Tool.Parent:FindFirstChildWhichIsA("Humanoid")
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if CanDamage and not Deb[Humanoid] and not hit.Parent:IsDescendantOf(Tool.Parent) and Humanoid then
		
		Deb[Humanoid] = true
		--HitSounds
		print(ComboCount .. " is the combo count")
		if Humanoid then
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - (Damage * ComboCount)
			if hit.Parent.Humanoid.Health < 1 then
				local tag = Instance.new("ObjectValue",Humanoid)
				tag.Name = "creator"
				tag.Value = MyHuman.Parent
				print(tag.Value)
			end
		end
		if ComboCount == 1 then
			slash1hit:Play()
			
		end
		if ComboCount == 2 then
			slash2hit:Play()
			
		end
		if ComboCount == 3 then
			slash1hit:Play()
		end
		if ComboCount == 4 then
			slash3hit:Play()
		end
		CanDamage = false
	end
end)


Tool.Equipped:Connect(function()
	MyRoot = Tool.Parent:FindFirstChild("HumanoidRootPart")
	local humanoid = Tool.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then

		idleAnim1 = humanoid:LoadAnimation(idle)
		idleAnim1:Play()

		--// Check for movement
		Event_ = humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()

			local magnitude = humanoid.MoveDirection.Magnitude

			if magnitude <= 0.1 and state ~= "idle" then
				state = "idle"
				idleAnim1 = humanoid:LoadAnimation(idle)
				idleAnim1:Play()
				
				if idleAnim2 then
					idleAnim2:Stop()
				end

			elseif state ~= "other" then
				state = "other"
				idleAnim2 = humanoid:LoadAnimation(run)
				idleAnim2:Play()
				if idleAnim1 then
					idleAnim1:Stop()
				end
			end
		end)
	end
end)
	--if idleAnim1 then
	--	idleAnim1:Stop()
	--end


	--else		
	--print("No Humanoid Detected")		
	--end
	--end)
--end)



Tool.Activated:Connect(function()
	local humanoid = Tool.Parent:FindFirstChildWhichIsA("Humanoid")
	local playanim = humanoid:LoadAnimation(idle)
	if Debounce then
		Debounce = false
		CanDamage = true
		Deb = {}
		local ExtraDelay = 0
		if tick()-LastSwing > 4 or ComboCount >= 4 then
			--reset
			ComboCount = 1
			if ComboCount >= 3 then
				ExtraDelay = 2
			end
		else
			--increase combo
			ComboCount = ComboCount + 1
			if ComboCount >= 4 then
				ExtraDelay = 2
			end	
		end
		if ComboCount == 1 then
			slash1miss:Play()
		end
		
		if ComboCount == 2 then
			slash2miss:Play()
			
		end
		if ComboCount == 3 then
			slash1miss:Play()
			
		end
		if ComboCount == 4 then
			slash3miss:Play()
		end
		playanim:Stop()
		AnimationRemote:FireAllClients(ComboCount)
		LastSwing = tick()
		
		wait(.3)
		CanDamage = false
		
		wait(ExtraDelay)
		Debounce = true
	end
end)


Tool.Unequipped:Connect(function()
	Event_:Disconnect()
	if idleAnim1 then idleAnim1:Stop() end
	if idleAnim2 then idleAnim2:Stop() end
end)

LeaderStats Script:

local players = game.Players

players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Kills = Instance.new("IntValue",leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
	
	local deaths = Instance.new("IntValue",leaderstats)
	deaths.Name = "Deaths"
	deaths.Value = 0
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
		Humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local Tag = Humanoid:FindFirstChild("creator")
			local killer = Tag.Value
			print(killer)
			if Tag and killer then 
				game.Players:GetPlayerFromCharacter(killer).leaderstats:FindFirstChild("Kills").Value = game.Players:GetPlayerFromCharacter(killer).leaderstats:FindFirstChild("Kills").Value + 1
				--killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
			end
		end)
	end)
end)
1 Like

this is the change in the sword script