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
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
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)
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.