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

Rewarding Players For Killing NPC's?

Asked by
dyler3 1510 Moderation Voter
9 years ago

So, recently I've been working on creating my own NPC's and all that stuff...well anyways, my question to whoever is willing to help is: How do I find the player who killed the NPC? How would I be able to do this, and find the exact player who killed the NPC so that I can award them? (I want the player to be awarded gold when the NPC is killed.)

Gold Stat: game.Players.Player.leaderstats.Gold

Heres what I have so far in the sword I made:

Plr=game.Players.LocalPlayer
Mouse=Plr:GetMouse()
Swinging=false
ReadyToSwing=true
SwingTime=.75

---Functions---
function Swing()
    Swinging=true
    ReadyToSwing=false
    script.Parent.Parent.GripForward=Vector3.new(1,0,0)
    script.Parent.Parent.GripPos=Vector3.new(0,0,-1)
    script.Parent.Parent.GripRight=Vector3.new(0,1,0)
    script.Parent.Parent.GripUp=Vector3.new(0,0,0)
    wait(SwingTime)
    script.Parent.Parent.GripForward=Vector3.new(1,0,0)
    script.Parent.Parent.GripPos=Vector3.new(0,0,-1.5)
    script.Parent.Parent.GripRight=Vector3.new(0,-1,0)
    script.Parent.Parent.GripUp=Vector3.new(0,0,1)
    Swinging=false
    wait(1)
    ReadyToSwing=true
end

script.Parent.Touched:connect(function(Part)
    if Part.Parent:FindFirstChild("Humanoid") and Swinging==true then
        Plr=Part.Parent
        PlrName=Plr.Name
        if not game.Players:FindFirstChild(PlrName) or game.Players:FindFirstChild(PlrName).Character~=Plr then
            Plr.Humanoid:TakeDamage(math.random(5,8)/2) 
        end
    end         
end)

script.Parent.Parent.Equipped:connect(function()
    Mouse.Button1Down:connect(function()
        if Swinging==false and ReadyToSwing==true then
            Swing()
        end
    end)
end)

If anyone could possibly help me, thank you in advance.

(If any more information is needed, please leave a comment.)

1 answer

Log in to vote
1
Answered by
yumtaste 476 Moderation Voter
9 years ago

The way I do this is by having an ObjectValue called "attacker" with the value of the player be inserted into the NPC's Humanoid. Then, in the NPC, there's an event that listens for .Died(), and it will use the "attacker" ObjectValue to find the player's leaderstats, and then add gold. Maybe a little something like this? (Note: this doesn't have to be a new script. It can be added into the function on line 25)

--in the object that will damage the NPC upon hitting it

script.Parent.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
local humanoid = part.Parent:FindFirstChild("Humanoid")
local attacker = Instance.new("ObjectValue", humanoid)
attacker.Name = "attacker"
attacker.Value = Plr
attacker.Parent = humanoid
Debris:AddItem(attacker, 0.5) --you can change this part, maybe add something to delete everything called "attacker" in humanoid before inserting it, this part just makes it automatically remove itself after 0.5 seconds.
end
end
end)
--in NPC's Humanoid

script.Parent.Died:connect(function()
local attacker = script.Parent:FindFirstChild("attacker")
if attacker then
attacker.Value.leaderstats.Gold.Value = attacker.Value.leaderstats.Gold.Value
end
end)

If this code doesn't work, or you need more help, leave a comment!

1
Yes, this works :D. Thanks for the response. Thumbs up :P dyler3 1510 — 9y
0
Thanks. More people should ask questions like you do. Your question was very thorough and easy to read, and allowed me to give you an answer quickly and easily. yumtaste 476 — 9y
Ad

Answer this question