"ENT:Touch" not functioning properly

I am currently developing an entity for a certain gamemode and I want to run a certain script when a player collides with said entity

So far, after writing my own code for this, the entity works fine and rotates, yet it fails to do anything in the “ENT:Touch” hook.

Here is what I have so far in the init.lua file:

[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

function ENT:Initialize()

self:SetModel( "models/props_junk/watermelon01.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_NONE )
self:DrawShadow(true)

end

function ENT:Use( activator, caller )
return
end

function ENT:Think()

self:SetAngles( self:GetAngles() + Angle(0, 30, 0)

end

function ENT:StartTouch( ent )
if ( ent:IsValid() and ent:IsPlayer() ) then
print(“Melon get!”)
self:Remove()
end
end
[/lua]

try adding this [lua] self:SetTrigger( true ) [/lua] to your Initialize() funciton.

I used this on a remake of trigger_teleport, but it was set to self:SetSolid( SOLID_BBOX ) so this may not work for the reason _nonSENSE said.

Not sure if ENT:Touch works with SOLID_NONE.

I was thinking the same thing, but will try Fantym420’s idea

[editline]17th January 2011[/editline]

Argh same result

[editline]17th January 2011[/editline]

I tried setting self:setSolid to SOLID_VPHYSICS but same result

Set the collision group, along with SOLID_VPHYSICS. http://wiki.garrysmod.com/?title=Entity.SetCollisionGroup

What would I set the gollision group to? Would COLLISION_GROUP_DEBRIS work?

COLLISION_GROUP_PLAYER would make it only collide with players, so I would use that.

Gracias, giving it a try.

[editline]17th January 2011[/editline]

Well after setting it to COLLISION_GROUP_PLAYER, the melon is unable to spawn. It simply spits, “Attempted to create unknown entity type melon”

[editline]17th January 2011[/editline]

Here is the revised code:

[lua]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include(‘shared.lua’)

function ENT:Initialize()

self:SetModel( "models/props_junk/watermelon01.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_VPHYSICS )
self:SetCollisionGroup(COLLISION_GROUP_PLAYER)
self:DrawShadow(true)

self:SetTrigger( true )

end

function ENT:Use( activator, caller )
return
end

function ENT:Think()

self:SetAngles( self:GetAngles() + Angle(0, 30, 0)

end

function ENT:Touch( ent )
print(“Touched!”)
if ( ent:IsValid() and ent:IsPlayer() ) then
print(“Melon get!”)
self:Remove()
end
end
[/lua]

Your console should show an error somewhere near the top.

Why is that?

I still get the error “Attempted to spawn unknown entity type” nothing spawns

There is an error in your console because your entity is failing to load do to some syntax error. Just copy/paste your entire console.

Edit* actually I can see your syntax error now, in your Think() function your missing a ) at the end of SetAngles()