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

Having troubles awarding currency...?

Asked by 10 years ago

I'm making a game and gold is the currency. I want to make NPC's that if you kill you will be awarded a certain amount of gold. I currently don't have the knowledge to do this, all I have is this script:

amnt = 50000
function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if (h~=nil) then
        local thisplr = game.Players:findFirstChild(h.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local score = stats:findFirstChild("Gold")
                if (score~=nil) then
                    score.Value = score.Value + amnt
                end
            end
        end
        script.Parent:remove()
    end
end

script.Parent.Touched:connect(onTouched)

I am a extreme newbie to Lua so I tried taking the easy way out and just making it so if you touch the NPC then it will award you the gold; but that causes many problems. Could anyone help me? Greatly appreciated.

1 answer

Log in to vote
2
Answered by 10 years ago

Put an ObjectValue inside of the NPC model. I named the value "Attacker". Every time that the NPC is attacked change the ObjectValue to the attacker. Then using the Diedevent of the Humanoid. Just award the person inside of the Objectvalue the points.

This would be located in the character model

local Amount = ???

local Dead = false
local Char = script.Parent
local Attacker = Instance.new("ObjectValue",Char)
Attacker.Name = "Attacker"

function WhenTouched(Part)
    if Part:IsA("Part") and Part.Parent:IsA("Tool") then
        --This is executed when a sword hits him
        if not Dead then
            Attacker.Value = Game.Players:GetPlayerFromCharacter(Part.Parent.Parent)
        end
    end
end


function Respawn()
    --Respawn here
    Dead = false
end

Char.Humanoid.Died:connect(function()
    Dead = true
    Attacker.Value.leaderstats.Gold = Attacker.Value.leaderstats.Gold + Amount
    Respawn()
end)

for _,n in pairs(Char:GetChildren()) do
    if n:IsA("Part") then
        n.Touched:connect(WhenTouched)
             --Fire WhenTouched whenever any part of the character is touched.
    end
end
Ad

Answer this question