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?
Target = script.Parent.Value Me = script.Parent.Parent RS = script.Parent["Right Shoulder"] Safe = {"ComputerComplexity"} script.Parent.Parent.Humanoid.WalkSpeed = 10 function Follow(Part) if Part.Parent:FindFirstChild("Humanoid") ~= nil then Target.Value = Part.Parent.Name for i = 1, 20 do Me.Humanoid:MoveTo(Part.Parent.Torso.Position, Part.Parent.Torso) wait(0.025) end end end function Punch(Hit) for i = 1, 10 do end end script.Parent.Touched:connect(Follow) script.Parent.Touched:connect(Punch)
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.
To add on to OzzyFin's answer:
Inside the script that kills the NPC (from the player):
function onDamage(humanoidYouDamaged) local new = Instance.new("ObjectValue",humanoidYouDamaged) new.Name = "creator" new.Value = thePlayerThisScriptWorksFor game.Debris:AddItem(new,1) -- deletes this object after 1 second end -- fire this function whenever you damage an NPC, 'onDamage(humanoidObject)'
Add this code on Line 16
of your script:
local human = Me.Humanoid local statName = "Cash" -- name of the stat human.Died:connect(function() local tag = human:FindFirstChild("creator") if tag then local stats = tag.Value:FindFirstChild("leaderstats") if stats and pcall(stats["StatName"].Value = stats["StatName"].Value) == true then stats["StatName"].Value = stats["StatName"].Value + 50 end end end)
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