Help with sent

I’m trying to make a sent for my race gamemode where it is a wooden crate and when touched it kill the person doing so. I’m having problems trying to spawn the entity and I am unsure if I am even making it right. I’m using the go fish gamemode to get a better understanding on what to do but still unsure. Any help will be greatly appreciated.

Make sure you have the right hierarchy.
I’m going to assume you have this entity in your gamemode folder.
Make sure you have your shared.lua set up like this:
[LUA]
ENT.Base = “base_anim”
ENT.Type = “anim”

ENT.PrintName = " " //Name of your ENT
ENT.Category = " " //Section in your spawnmenu the ENT is in. Not needed.
ENT.Author = " " //Your name
ENT.Contact = " " //Your E-Mail
ENT.Purpose = " " //What it’s used for
ENT.Instructions = " " //Self-explanatory

ENT.Spawnable = true //Will it be in your spawn menu?
ENT.AdminSpawnable = true //Will it be in an Admin’s spawn menu?
[/LUA]

I’m also going to assume you have your init.lua all set up.
If not, just tell me so I can help you :slight_smile:

Anyway, make sure your touch script looks like this.
[LUA]
function ENT:Touch( hitEntity )
if ( hitEntity:IsValid() and hitEntity:IsPlayer() ) then
hitEntity:Kill()
end
end
[/LUA]

Also make sure it’s actually hitting the player, and not the car.
Because the code mentioned above will only work if it touches the player.

Creating an entity :
**[Ents.Create

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Ents.Create)**

Getting the driver of a vehicle :
**[Entity.IsVehicle

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.IsVehicle)**
**[Vehicle.GetDriver

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Vehicle.GetDriver)**

Thats great thanks, I was looking at using Ents.Create but does im unsure if there is anything extra to do if its a sent and not an entity.
Is this what i would do roughly?

bomb=ents.Create(sent_bomb)
bomb:SetModel("models/props_junk/wood_crate001a.mdl")
bomb:SetPos(Vector(0,0,0)
bomb:Spawn()

With the sent all sent stuff set up. One more question would be is it better to set what the model will be in the sent init or would it be better to do it in the gamemode init? Oh and if possible could you show me how to set up the init I want to make sure i got everything right. Thanks again

Can’t type a ton right now so quick answers :
The code in your last post is correct
I would set the model in ENT:Init()
You should find a simple sent and look at it for an example of a proper entity initialization. Note that ENT:SpawnFunction() is only used in sandbox.

Just for future reference, a SENT is an Entity.

SENT stands for scripted entity :wink:

yeah I know I was just wondering if you would have to add SetKeyValue of something but I got it working thanks.

Instead of making a new post I thought i might as well put it here. Im having trouble using right click to spawn the bomb.

init.lua

concommand.Add("rgm_spawnbomb", function(ply)
    jeep = ply:GetVehicle()
    bomb=ents.Create("sent_bomb")
    bomb:SetPos(jeep:GetPos()+jeep:GetForward()*-140+ Vector(0,0,20))
    bomb:Spawn()
    bomb:Activate()
end)

and the cl_init.lua

function GM:Think()
    if(input.IsMouseDown(Mouse_Right)) then
        if(rcdown == false) then
            RunConsoleCommand("rgm_spawnbomb")
            rcdown = true
        end
    else
        rcdown = false
    end
end

and rcdown is stated earlier as false

The problem is that it gives me this error RaceGM/gamemode/init.lua:245: Tried to use a NULL entity! which is this line

bomb:SetPos(jeep:GetPos()+jeep:GetForward()*-140+ Vector(0,0,20))

and the box will not spawn with right click but it will spawn if i type in the console command.

I’m not really sure why you’re getting this error, especially if you’re saying it works when you type the console command manually.

Anyway there are a few things you could improve in your script, first of all there is no need to do anything clientside or even to have a console command at all since it could easilly be abused by the client. So let’s do everything serverside.
Next it would be nice to put local in front of your variable so that you can freely re-use these names elsewhere and to reduce the chance of addon conflicts.
I also suggest you do input detection using **[Gamemode.KeyPress

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.KeyPress)**, and finally please start using [lua] tags instead of

! It annoys me everytime I quote you. :wink:

Your code would now look like that :

init.lua
[lua]local function SpawnBomb(ply)

	local jeep = ply:GetVehicle()
		
	if not ValidEntity(Jeep) then return end -- Make sure that jeep is a valid entity, if not terminate execution
		
	local bomb = ents.Create("sent_bomb")
	print(jeep,bomb) -- This is for debug purposes, get rid of it later
	bomb:SetPos( jeep:GetPos() + jeep:GetForward() * -140 + Vector(0,0,20) )
	bomb:Spawn()
	bomb:Activate()
	
end

	
function GM:KeyPress(ply,key) -- You can easilly do the rest of your key detection here
	if key == IN_ATTACK2 then
		SpawnBomb(ply) -- If right click was pressed then we can call our SpawnBomb function for that player
	end
end[/lua]

[editline]04:52PM[/editline]

Added an example of how to put a delay between bombs, just incase :
[lua]local delay = 2 -- Delay of 2 seconds between each bombs

local function SpawnBomb(ply)

	if CurTime() < ply.LastBomb + delay then return end -- A delay between bombs
	
	local jeep = ply:GetVehicle()
		
	if not ValidEntity(Jeep) then return end -- Make sure that jeep is a valid entity, if not terminate execution
		
	local bomb = ents.Create("sent_bomb")
	print(jeep,bomb) -- This is for debug purposes, get rid of it later
	bomb:SetPos( jeep:GetPos() + jeep:GetForward() * -140 + Vector(0,0,20) )
	bomb:Spawn()
	bomb:Activate()
	
	ply.LastBomb = CurTime()
	
end

	
function GM:KeyPress(ply,key) -- You can easilly do the rest of your key detection here
	if key == IN_ATTACK2 then
		SpawnBomb(ply) -- If right click was pressed then we can call our SpawnBomb function for that player
	end
end

function GM:PlayerInitialSpawn(ply)
	ply.LastBomb = 0 --Initializing this value only once is enough
end[/lua]

Thanks you, this is perfect, THANK YOUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
oh and ill use lua tags now :smiley:

Now how would you make an entity move forward until it hits anything? I can make it move forward but it has gravity still and also i don’t know how to make it register touch with the world. Any help pointing in the right direction would be great.

To disable gravity :
**[Entity.GetPhysicsObject

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.GetPhysicsObject)**
**[PhysObj.EnableGravity

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=PhysObj.EnableGravity)**

To detect when your sent touches something :
**[ENT.Touch

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=ENT.Touch)**

:worship: **[The wiki

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/)**

I can’t seem to detect collision with the world. Touch only handles entity. Thanks for the help so far though, really made this take alot less time.