Only press E to pickup weapon, lua error


hook.Add( "PlayerSpawn", "PickupTimeout", function( ply )
	ply.PickupTimeout = CurTime() + 0.5
end )
hook.Add( "PlayerCanPickupWeapon", "NoPickup", function( ply, wep )
	if ( ( ply.PickupTimeout or 0 ) < CurTime() ) then return false end
end )

hook.Add( "KeyPress", "PressUse", function( ply, key )
	if ( key == IN_USE ) then
		local tr = ply:GetEyeTrace()
		if ( ValidEntity( tr.Entity ) and tr.Entity:IsWeapon() and tr.Entity:GetPos():Distance( ply:GetShootPos() ) < 96 ) then
			ply.PickupTimeout = CurTime() + 0.5
			
			ply:Give( tr.Entity:GetClass() )
			tr.Entity:Remove()
		end
	end
end )

Can’t figure it out. Here’s the error.


[ERROR] lua/autorun/weaponpickup.lua:12: attempt to call global 'ValidEntity' (a nil value)
  1. v - lua/autorun/weaponpickup.lua:12
   2. unknown - lua/includes/modules/hook.lua:82

It also disables me from instantly getting the weapon I want by just left-clicking it.

IsValid instead of ValidEntity