How to get player from script?

I just want to know how I would find the player to locate leaderstats so I can add money to the player everytime the npc dies.

local Enemy = script.Parent.Humanoid
function KillForXp() 
	local tag = Enemy:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
				local leader = tag.Value:FindFirstChild("Quest")
				if leader.Type.Value == 1 then
					if leader ~= nil then
						leader.Amount.Value = leader.Amount.Value + 1
						
				end 
			end 
		end
	end
end 
Enemy.Died:connect(KillForXp) 

Unfortunately this is something you’d have to integrate directly into the scripts for your tools. Humanoid damage isn’t tied to any specific player by default.

1 Like

Try this one it will find the player and its leaderstats as what youve said:

local Enemy = script.Parent.Humanoid
function KillForXp() 
	local tag = Enemy:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
                            local plr = game.Players[tag.Value] ---find the player
                            if plr then --lets be sure it find the player
				local leader = plr.leaderstats:FindFirstChild("Quest") --pretty sure quest is found in player leaderstats
				if leader.Type.Value == 1 then
					if leader ~= nil then
						leader.Amount.Value = leader.Amount.Value + 1
					end
				end 
			end 
		end
	end
end 
Enemy.Died:connect(KillForXp)
1 Like