Scripted Entity not responding to physgun - please help?

Hi all,

Sorry about asking for help (looking at other posts, that isn’t allowed on here!) but if anyone knows what’s going on, I’d like their input.
I’ve recently (about three months ago-ish?) started to learn lua coding as used in GMod, and after boring of “soopuh leet admin gun shoto alot lolol” fairly quickly I tried to write a scripted entity.

The entity is supposed to be a watermelon. It’s edible to restore 50 health. It has 200hp itself and explodes when destroyed.
The problem I’m having is the entity not responding to the physgun - I spawned it on gm_flatgrass, on top of the spawn platform, and it failed to respond to the physgun completely. As in, the beam changes colour and locks onto the watermelon, but turning the view produces no result whatsoever. The watermelon stays firmly in place. It also refuses to be moved with the Gravity Gun, which doesn’t acknowledge its presence whatsoever, and seems to have no collisions (I can walk straight through it).

After several seconds, it teleports a short way away and begins responding normally to the physgun; however, the beam stays locked onto the spot it teleported to instead of the watermelon’s apparent location. It begins to collide normally.

Spawning additional watermelons doesn’t seem to cause the same problem, but they will still refuse to be moved for about 1-3 seconds when spawned, and the physgun beam will lock onto where it was spawned rather than its apparent location.

Code as follows:

[lua]AddCSLuaFile()

DEFINE_BASECLASS( “base_anim” )

ENT.PrintName = “Watermelon”
ENT.Author = “abcq2”
ENT.Information = “nom”
ENT.Category = “Practical”

ENT.Editable = true
ENT.Spawnable = true
ENT.AdminOnly = false
ENT.RenderGroup = RENDERGROUP_TRANSLUCENT

ENT.MelonHealth = 200
ENT.MelonActive = true

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 16

local ent = ents.Create( "sent_watermelon" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
	
return ent

end

function ENT:OnTakeDamage(dmg)
if !self.MelonActive then return end
self.MelonHealth = self.MelonHealth - dmg:GetDamage()
–print(self.MelonHealth)
if (self.MelonHealth <= 0) then
self.MelonActive = false
self.Entity:Destruct()
end
end

function ENT:Initialize()
self.Entity:SetModel(“models/props_junk/watermelon01.mdl”)
self.Entity:PhysicsInit(SOLID_VPHYSICS)
self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
self.Entity:SetSolid(SOLID_VPHYSICS)

local physics = self.Entity:GetPhysicsObject()
if (physics:IsValid()) then
	physics:Wake()
end

end

function ENT:Use(activator,caller)
if (!activator:IsValid()) then return end

self:Remove()

if ( activator:IsPlayer() ) then
	-- Give the collecting player some free health
	local health = activator:Health()
	activator:SetHealth( health + 50 )
end

end

function ENT:Destruct()
self:Remove()
local explode = ents.Create( “env_explosion” )
explode:SetPos( self.Entity:GetPos() )
explode:SetOwner( self.Owner )
explode:Spawn()
explode:SetKeyValue( “iMagnitude”, “800” )
explode:Fire( “Explode”, 0, 0 )
explode:EmitSound( “weapon_AWP.Single”, 400, 400 )
return
end[/lua]

Any input appreciated (including input on where I’m supposed to post this!)

Thanks

Try this:



function ENT:Initialize()
    if CLIENT then return end
    self:SetModel("models/props_junk/watermelon01.mdl")
    self:PhysicsInit(SOLID_VPHYSICS)
    self:SetMoveType(MOVETYPE_VPHYSICS)
    self:SetSolid(SOLID_VPHYSICS)

    local physics = self:GetPhysicsObject()
    if ( IsValid(physics) ) then
        physics:Wake()
    end
end

And don’t use self.Entity., it’s deprecated, use just self.

That worked. Cheers! :smile:
And thanks for the tip about self.Entity, will do that in the future.