Adding players time to the killers time

Imagine that Player A had 1000 time and Player B has 0 time.
If Player B kills Player A, Player B’s time should be 1000.
Because I want to add the victims time to the killers time.

I looked through the SwordScript by Roblox and found that when the sword attacks and hits someone, a object value is added with the object being the player that was hit

But adding kills also works

No errors as well

local plrs = game:GetService("Players")

plrs.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        hum.Died:Connect(function()
            plr.leaderstats.Time.Value = 0
            for _, v in ipairs(hum:GetChildren()) do
                if v:IsA("ObjectValue") and v.Value and v.Value:IsA("Player") then
                    local killer = v.Value
                    local leaderstats = killer:WaitForChild("leaderstats")
                    local Kills = leaderstats:WaitForChild("Kills")
                    local Time = leaderstats:WaitForChild("Time")
                    if killer:FindFirstChild("leaderstats") and Killer.leaderstats:FindFirstChild("Kills") then
                        Kills.Value += 1
                        Time.Value += plr:WaitForChild("leaderstats").Time.Value
                    end
                end
            end
        end)
    end)
end)

The sword script: (this is just a little snippet of the code)

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
1 Like

First of all, dont really think ipairs is necessary.

You sure you’re actually calling the function?

1 Like
function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)	
end

Yes they are calling the function, and i also changed ipairs to pairs

1 Like

If u need the full sword script, then i can show it

1 Like

Have you tried adding some print statements after these if statements? If not do that and see where it stops.

If you gonna work with other devs in the future, I’d recommend using elseif statements and indenting the lines

1 Like

I’ll add print statements, but this was the free model sword by Roblox just adding

1 Like

image

All print statements worked

local plrs = game:GetService("Players")

plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		hum.Died:Connect(function()
			plr.leaderstats.Time.Value = 0
			for _, v in ipairs(hum:GetChildren()) do
				if v:IsA("ObjectValue") and v.Value and v.Value:IsA("Player") then
					local killer = v.Value
					print("FOUND KILLERS")
					local leaderstats = killer:WaitForChild("leaderstats")
					local Kills = leaderstats:WaitForChild("Kills")
					local Time = leaderstats:WaitForChild("Time")
					print("FOUND ALL LEADERSTATS VALUES")
					if killer:FindFirstChild("leaderstats") and killer.leaderstats:FindFirstChild("Kills") then
						Kills.Value += 1
						print("KILLS ADDED")
						Time.Value += plr:WaitForChild("leaderstats").Time.Value
						print("TIME ADDED")
					end
				end
			end
		end)
	end)
end)

Print out the value of Time before and after the statement that adds more. And also this is a Server-Sided Script right?

1 Like

NO WAIT ITS A SERVER SCRIPT

oopsies

1 Like

I found the problem,
In the code, when the player dies, the time is being set to 0 before I add the time.
I believe i just need to put that piece of code after everything.

1 Like

Nice then mark your post as the solution afterwards!

1 Like

Thanks for helping! Adding print statements definitely made me catch the problem!

1 Like

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