(LUA) How do i make an entity pickup-able?

I’m not very experienced with LUA so im not sure how i am able to do this.
I’ve created my own version of a ball script (which creates a ball entity), and i want the ball to be able to be picked up with the Use key like an ordinary prop.
The only problem is that i don’t know how i can achieve this or if it CAN be achieved at all.
I think it has to do with




or something, but im none-the-wiser.

Help with this would be much appreciated.

Yeah, you’d have to use those two together. But, you would have to also as well use the SIMPLE_USE on the USE hook so it doesn’t get called thousands of times.

geferon is half right. The ball entity in sandbox is removed when you use it so the entity’s use type doesnt really matter. For and entity that you want to check when it gets used (i.e. pressing ‘e’ on it) use


I tried using this, but the ball entity doesn’t show up in the spawnmenu anymore:

function ENT:Use( activator, caller )

	if ( activator:IsPlayer() ) then
		function PlayerPickupObject( ply, obj )
		if ( obj:IsPlayerHolding() ) then return end
		ply:PickupObject( obj )


Any suggestions?

You’re missing an “end” to close the if( activator:IsPlayer() ) then block

If you have any lua errors, the item won’t show up in your spawn menu and you’ll have to change level or restart Garry’s Mod to get it to show up again

It worked, but when i try to pick up the ball entity, i get this error in the console:

[ERROR] addons/funball/lua/entities/funball.lua:98: attempt to index global 'ply' (a nil value)
  1. unknown - addons/funball/lua/entities/funball.lua:98

-snip- (Retard)

Are you trying to call


Edit: Okay, I will just guess that you are.

By putting “function” anywhere in your code, you are telling gmod that you want a NEW function to be made. Not what you want. You want to call Player:PickupObject(entity) (also, you are missing the colon)

Edit #2: ply is nil because it is never defined. In place of ‘ply’ use ‘activator.’

I think this is what you’re going for.

If it is, please don’t just take it blindly because that won’t teach you what you did wrong. Read it and figure out how it’s different and where you went wrong so you can write cool, working code going forward

function ENT:Use( activator, caller )
	if ( activator:IsPlayer() ) then
		if ( self:IsPlayerHolding() ) then return end
		activator:PickupObject( self )

It worked perfectly, thanks a bunch.
Don’t worry, I’ll be sure to look carefully at what i did wrong so i can write better in the future (despite this being a one-time code that i need).