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

I made a NPC, if he dies player who killed him will be rewarded, how do i find the player?

Asked by 9 years ago

So I made an NPC, so when he dies he is supposed to reward the player who killed him 50 cash, but the thing is, how do you find the player?

01Target = script.Parent.Value
02Me = script.Parent.Parent
03RS = script.Parent["Right Shoulder"]
04Safe = {"ComputerComplexity"}
05script.Parent.Parent.Humanoid.WalkSpeed = 10
06function Follow(Part)
07    if Part.Parent:FindFirstChild("Humanoid") ~= nil then
08        Target.Value = Part.Parent.Name
09        for i = 1, 20 do
10            Me.Humanoid:MoveTo(Part.Parent.Torso.Position, Part.Parent.Torso)
11 
12            wait(0.025)
13        end
14    end
15end
View all 23 lines...

2 answers

Log in to vote
2
Answered by
ozzyDrive 670 Moderation Voter
9 years ago

The way ROBLOX default weapons and linked leaderboard handle this is inserting a tag (an ObjectValue) into the humanoid they damage, giving its value the player object and whenever any of the humanoids' died event fires it reads the tag's value (if there's one) & awards the player.

However I and probably many others nowadays do it by just checking if the health of a humanoid getting damaged goes under 0 and reward the client which told server to fire a projectile to this direction or something similar.

It's not handled through scripts inside the NPC (until you do it the ROBLOX style, though it also of course requires inserting the tag from seperate script), but rather from the scripts doing the damage.

Ad
Log in to vote
0
Answered by 9 years ago

To add on to OzzyFin's answer:

Inside the script that kills the NPC (from the player):

1function onDamage(humanoidYouDamaged)
2    local new = Instance.new("ObjectValue",humanoidYouDamaged)
3    new.Name = "creator"
4    new.Value = thePlayerThisScriptWorksFor
5    game.Debris:AddItem(new,1) -- deletes this object after 1 second
6end
7 
8-- fire this function whenever you damage an NPC, 'onDamage(humanoidObject)'

Add this code on Line 16 of your script:

01local human = Me.Humanoid
02local statName = "Cash" -- name of the stat
03 
04human.Died:connect(function()
05    local tag = human:FindFirstChild("creator")
06    if tag then
07        local stats = tag.Value:FindFirstChild("leaderstats")
08        if stats and pcall(stats["StatName"].Value = stats["StatName"].Value) == true then
09            stats["StatName"].Value = stats["StatName"].Value + 50
10        end
11    end
12end)

The pcall basically checks to see if StatName exists. The Died event is when a humanoid dies, and the rest I believe you understand.

Hope I helped :)

~TDP

Answer this question