How can i get the entity's holder

I’ve seen how to see if the entity is held and other things, but i can’t find how to know who is the holder.
Thx for answering :slight_smile:

Player:GetActiveWeapon?

Need more information.

-snip-

That gets the current weapon a player is holding. I’m pretty sure he wants to know not the name of the weapon but the name of the player.

You’d have to make your own script to do this since a function like this doesn’t exist by default, try using the PlayerUse hook (and look around for some kind of entity-dropping hook)… I remember a thread EXACTLY like this one that I posted an example on before, but I can’t find it :frowning:

Thx dude in fact what i would want is a fonction that will be like this: Entity:GetPlayerHolding() that returns the holder but the best i have is Entity:IsPlayerHolding() returning a boolean…
What i’m doing is that when a player put trash (my entity) in a bin(the entity that’s thinking) he earns money.
Does Player:GetActiveWeapon works when u’r holding an entity pressing E?
Because if it’s works on simple entities i can do with it (but it will not be optimized at all).

So is it an entity, or a weapon? Because those are two completely seperate things.

I assume it’s an entity, so you can use the PlayerUse hook.

[lua]
hook.Add(“PlayerUse”, “playeruse_money”, function(ply, ent)
if ent:GetClass() == “put your entity name here” && !ent.HeldBy then
ent.HeldBy = ply
end
end )
[/lua]

and then you can use ent.FindInBox or the ent.FindInX functions to check if it’s in the bin.

example:

[lua]
hook.Add(“Think”, “money_checkifintrash”, function()
for k,v in pairs(ents.FindInBox(Vector(min, max))) do
if v:GetClass() == “again, your entity name here” then
if v.HeldBy then
local ply = v.HeldBy
ply:addMoney(put money you want to give here)
v.HeldBy = false
v:Remove() // remove the entity
end
end
end
end )
[/lua]

there is probably way more efficient ways, but this is all i
can think of right now

You’ve all understood :slight_smile: seem’s it’s the way to do it. Thanks dude i’m trying it and will put resolved if it works.

glad to help let me know if it works

Hum i don’t know if i’m doing it well…
i replace ( wich is correctly working).


	for _, Trash in pairs(ents.FindByModel("models/sligwolf/garbagetruck/sw_trashbag.mdl")) do
		if Trash:GetPos():Distance(ent:GetAttachment(3).Pos) <= 50 then		
		Trash:EmitSound("physics/metal/metal_box_break2.wav", 100, 100)
		Trash:Remove()
 			
		end
	end

by:


	for _, Trash in pairs(ents.FindByModel("models/sligwolf/garbagetruck/sw_trashbag.mdl")) do
		if Trash:GetPos():Distance(ent:GetAttachment(3).Pos) <= 50 then
			if Trash.HeldBy then
              			local ply = Trash.HeldBy 
				ply:addMoney(100)
				notification.AddLegacy( "Vous empochez 100€ pour avoir collecté la poubelle", NOTIFY_GENERIC, 2 )
			end			
			Trash:EmitSound("physics/metal/metal_box_break2.wav", 100, 100)
			Trash:Remove()
 			
		end
	end

wich did the same thing so isn’t working like i would…
Oh and i don’t know how to integrate notification.AddLegacy in this server-side script…

not sure what to do. and you’ll need to network notification.addlegacy or use a console command

In fact it’s because v.HeldBy is always returning a nil value :confused:

in the entity file (in init.lua) in ENT:Initialize, add self.HeldBy = 0 and then check if it’s 0

it’s 0 (that take me alot f time because i’m on sigwolf’s garbage truck that don’t use this king of model)