How to use world entity??

So I created a entity when all entities have initialized but when I place function box:Use(c , a) it keeps saying box is a nill value do I have to put the function in the hook?

Full Code:

hook.Add( “InitPostEntity”, “some_unique_name”, function()
local box = ents.Create( “prop_physics” )
box:SetModel( “models/bonkaddict/apb/jokerammo/ammovendingmachine.mdl” )
box:SetPos( Vector( -98,-109,73 ) )
end )

function box:Use(c , a)
c:SetHealth(c:Health() + 50)
c:ChatPrint(“Eaten Bread + 50 Health.”)

Any help would be appreciated.

first off you’re initializing ‘box’ inside the function of the hook locally, so it would only be valid within that function.

secondly; your initialization of :use() is executing before your initPostEntity hook is, which means it’ll return nil if not placed within the hook itself

place the :use()inside the hook function itself

also side note; your health thing won’t work since you’re calling :use() on a prop_physics entity

Yeah I only have that health thing because I was only seeing if it would work(the function not the hp).

Thanks by the way :smiley: