I am currently having problem with these scripts. what i would like to achieve is that my tool Script creates an object value and then when the player dies the time should go to the other player. But the issue is that it only works once and then it does not give the time to the player who killed the other player.
I have already tried solutions on the devforum and searched on the internet.
-- Tool Script
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local Tool = script.Parent
local Handle = Tool.Handle
local playerWithTool
local Character
local Damage = 10
Handle.Touched:Connect(function(Hit)
local otherCharacter = Hit.Parent
if otherCharacter then
local otherPlayer = Players:GetPlayerFromCharacter(otherCharacter)
if otherPlayer and otherPlayer ~= playerWithTool then
local Humanoid = otherCharacter:FindFirstChildOfClass("Humanoid")
if Humanoid then
Humanoid:TakeDamage(Damage)
local Tagged = Instance.new("ObjectValue")
Tagged.Name = "creator"
Tagged.Value = playerWithTool
Tagged.Parent = Humanoid
Debris:AddItem(Tagged, 2)
end
end
end
end)
function Unequipped()
playerWithTool = nil
Character = nil
Tool.Grip = Up
end
-- Time Script
local Players = game:GetService("Players")
local function updateLeaderstats(player, leaderstatName, incrementAmount)
local leaderstatCheck = type(leaderstatName) == "string"
local incrementCheck = type(incrementAmount) == "number"
if
leaderstatCheck
and incrementCheck
then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local leaderstatToUpdate = leaderstats:FindFirstChild(leaderstatName)
if leaderstatToUpdate then
leaderstatToUpdate.Value = leaderstatToUpdate.Value + incrementAmount
end
end
end
end
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function()
local Humanoid = Character.Humanoid
local Creator = Humanoid:FindFirstChild("creator")
if
Creator
and Creator:IsA("ObjectValue")
and Creator.Value
then
updateLeaderstats(Creator.Value, "Kills", 1)
updateLeaderstats(Creator.Value,"TimeAlive",Player.leaderstats.TimeAlive.Value)
Player.leaderstats.TimeAlive.Value = 0
end
end)
end)
end)