I'm making a script that gives the player money when he kills a player and the player who died will loose money. But it doesn't work, I'm not sure why, I tried to debug it but I couldn't figure it out.
I am using a tagging system to find the player and who killed him.
Local script located in player starterpack (script that takes away the players money and gives the killer money when he dies):
local plr = game.Players.LocalPlayer
repeat wait(1) until plr:FindFirstChild("leaderstats")
local hum = plr.Character.Humanoid
local stats = plr:WaitForChild("leaderstats")
local team = stats:WaitForChild("Team")
local money = stats:WaitForChild("Money")
while wait(0.1) do
hum.Died:connect(function()
print("died") --Prints this
local tag = hum:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
print("killed") --Prints this
local Leaderstats = tag.Value:findFirstChild("leaderstats")
if Leaderstats.Money.Value >= 100 then
print("Take") -- Doesn't print this. so I'm guessing the if statment isn't working. But I made sure to make the players money stat 100
money.Value = money.Value - 100
Leaderstats.Money.Value = Leaderstats.Money.Value + 100
end
end
end
end)
end
Tag script:
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid ~= nil then
local tag = humanoid:findFirstChild("creator")
if tag ~= nil then
tag.Parent = nil
end
end
end
--There more to this script but that doesn't matter because I know the taging system works

6 comments
Ugh, I have the autocorrection. Sorry about that.
Hate*
Wow, thanks for trying to answer my question on your phone. I replaced that line of code and I get the error: "attempt to index a nil value" on that line.
nvm I fixed it thanks!
Np
How'd you fix the nil value error?