Wrong player gains kills upon killing with my custom sword gear

I want an IntValue called “Kills” in the leaderboard to increase when a player kills someone, I want the killer to receive the kill. I already have the leaderboard set up, but my code is only half working. It seems to be a logic issue that I don’t have much of an idea on resolving. I modeled this code after the “TagHumanoid()” and “UntagHumanoid()” functions from Roblox’s Linked Sword. My sword activates a script that deals damage to foreign humanoids that touch it before deactivating that script. This is the code from that script:

local Hitbox = script.Parent.Handle.hitbox
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end
function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

Hitbox.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA('Humanoid')
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Humanoid then
		UntagHumanoid(Humanoid)
		TagHumanoid(Humanoid, Player)
		Humanoid.Health = Humanoid.Health - 25
		wait(1)
	end
end)

and this is the leaderstats code:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local kills  = Instance.new ("IntValue")
	kills.Parent = 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:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function(Died)
			deaths.Value = deaths.Value + 1
			local creator = humanoid:FindFirstChild("creator")
			local foreignLeaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value ~= nil then
				foreignLeaderstats.Kills.Value = foreignLeaderstats.Kills.Value + 1
			end
		end)
	end)
end)

I based it off of the linked sword’s code which can be found in the toolbox (by Roblox). What’s happening is that the player who kills (the killer) is not receiving the kill, instead, the player who was killed is receiving it. The victim is not supposed to receive the kill. How would I fix this?

Video:

Any assistance is greatly appreciated!! Thank you for reading.

2 Likes

You are tagging the player who was hit. You should be using the tool’s Parent (tool holder) to do that instead

local Player = Players:GetPlayerFromCharacter(script.Parent.Parent)
1 Like

You are my savior, thank you so so much.

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