Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

My reward player on kill script isn't working?

Asked by 7 years ago

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

1 answer

Log in to vote
1
Answered by
FiredDusk 1466 Moderation Voter
7 years ago
Edited 7 years ago

On phone but, where u have the Variable "Leaderstats" I see you are trying to find the one who tagged the player. To do this, u need to on in the Players service.

local Leaderstats = game.Players:FindFirstChild(tag.Value).Leaderstats

Hope this helps bud! If it did, a upvote would be great!

0
Ugh, I have the autocorrection. Sorry about that. FiredDusk 1466 — 7y
0
Hate* FiredDusk 1466 — 7y
0
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. roblox99456789 104 — 7y
0
nvm I fixed it thanks! roblox99456789 104 — 7y
View all comments (2 more)
0
Np FiredDusk 1466 — 7y
0
How'd you fix the nil value error? Jxdenx 45 — 5y
Ad

Answer this question