How Can I use ENTITY:Touch ?

When I put this code in Server , always Error


function ENTITY:Touch( entity )
	self:EmitSound( "ambient/explosions/explode_" .. math.random( 1, 9 ) .. ".wav" )
	self:Remove()
end


 attempt to index global 'ENTITY' (a nil value)


How can I use this ?

Please help me

Change “ENTITY” to “ENT”.


attempt to index global 'ENT' (a nil value)


Can you show me your entire code?

Just


function ENT:Touch( entity )
	self:EmitSound("ambient/explosions/explode_" .. math.random( 1, 9 ) .. ".wav", 500, 200)
end

What exactly do you expect this to do?

ENT:Touch is a function that only works on entities. Just calling it anywhere in a piece of code won’t work, as ENT is undefined, unless your code is for an entity.

Where are you saving this?

Can you give me a Example?


AddCSLuaFile()

ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.PrintName = "Example"
ENT.Author = ""
ENT.Contact = ""
ENT.Purpose = ""
ENT.Instructions = ""
ENT.Spawnable = true
ENT.Category = "Examples"

if SERVER then
	
	function ENT:Initialize()
		
		self:SetModel( "models/props_borealis/bluebarrel001.mdl" )
		self:SetMoveType( MOVETYPE_VPHYSICS )
		self:SetSolid( SOLID_VPHYSICS )
		self:PhysicsInit( SOLID_VPHYSICS )
		self:SetTrigger( true )
		
	end
	
	function ENT:Touch( entity )
		
		self:EmitSound( "ambient/explosions/explode_" .. math.random( 1, 9 ) .. ".wav", 500, 200 )
		
	end
	
end

function ENT:Draw()
	
	self:DrawModel()
	
end

Put that in garrysmod/lua/entities/yourentity/shared.lua

You might want to change Touch to StartTouch so that it doesn’t play every frame that you’re touching it.