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:
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
if !self.MelonActive then return end
self.MelonHealth = self.MelonHealth - dmg:GetDamage()
if (self.MelonHealth <= 0) then
self.MelonActive = false
local physics = self.Entity:GetPhysicsObject() if (physics:IsValid()) then physics:Wake() end
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
local explode = ents.Create( “env_explosion” )
explode:SetPos( self.Entity:GetPos() )
explode:SetOwner( self.Owner )
explode:SetKeyValue( “iMagnitude”, “800” )
explode:Fire( “Explode”, 0, 0 )
explode:EmitSound( “weapon_AWP.Single”, 400, 400 )
Any input appreciated (including input on where I’m supposed to post this!)