Hit Detection

I’m making an !bantrain evolve plugin, And all is good so far but I need to find a way to work out when the train hits the player, And if that player is the player mentioned in the !bantrain command.

The lua code so far is this:

[lua]
/-------------------------------------------------------------------------------------------------------------------------
Fuck a player with a train
-------------------------------------------------------------------------------------------------------------------------
/

local PLUGIN = {}
PLUGIN.Title = “Bantrain”
PLUGIN.Description = “Choo Choo!”
PLUGIN.Author = “Divran/Overv/Matt J”
PLUGIN.ChatCommand = “bantrain”
PLUGIN.Usage = “[player] [time] [reason]”
PLUGIN.Privileges = { “bantrain” }

function PLUGIN:Call( ply, args )
if ( ply:EV_HasPrivilege( “bantrain” ) ) then
local players = evolve:FindPlayer( args, ply )

	for _, pl in ipairs( players ) do
		pl:SetMoveType( MOVETYPE_NONE )
		self:SpawnTrain( pl:GetPos() + pl:GetForward() * 1000 + Vector(0,0,50), pl:GetForward() * -1 )
	end
end
end

function PLUGIN:SpawnTrain( Pos, Direction )
local train = ents.Create( “code_ent” )
train:SetAngles( Direction:Angle() )
train:SetPos( Pos )
train:Spawn()
train:Activate()
train:EmitSound( “ambient/alarms/train_horn2.wav”, 100, 100 )
train:GetPhysicsObject():SetVelocity( Direction * 50000 )

timer.Create( "TrainRemove_"..CurTime(), 5, 1, function( train ) train:Remove() end, train )

end

local MyCodeEnt = {}

MyCodeEnt.Type = “anim”

if SERVER then
function MyCodeEnt:Initialize()
self:SetModel(“models/props_vehicles/train_boxcar.mdl”)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
end
function MyCodeEnt:StartTouch(ent)
if ent:IsPlayer() then
ent:ChatPrint(“I Touched You!!”)
end
end
else

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

end
scripted_ents.Register(MyCodeEnt, “code_ent”, true)

function PLUGIN:Menu( arg, players )
if ( arg ) then
RunConsoleCommand( “ev”, “bantrain”, unpack( players ) )
else
return “bantrain”, evolve.category.punishment
end
end

evolve:RegisterPlugin( PLUGIN )
// God that took a while
[/lua]

Any help is appreciated.

Store the supposed person to be killed in train, for example train.tokill = people. Then open a death hook and check if the victim is the person/people in table and the weapon is the train then do whatever you wanted to do.

The train wont kill them if the server has Godmode on, I need to see if the train has hit the right person, Then do what I want it to do with that player

Don’t use godmode, use sbox_plpldamage 1.

It’s like godmode except props can kill.

Fair enough, But Still…

I’ll try this method first, and post back if it doesn’t work properly.

Just check to see if it’s the player you’re after with **[ENT.StartTouch

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

That won’t work, he’s spawing a prop_physics, if he made a custom ent for the train that would be the perfect answer.

Hm, I’ll try to convert it to a Entity. But I have absolutely no idea how!

Google here I come!

Here’s an example of how to create and entity without an actual entity folder, it need to be run shared or the entity will be there but invisible. I don’t know if the plugin’s are run shared so this may not work, but you would’t need to create a whole new ent somewhere else if it is.
[lua]
local MyCodeEnt = {}

MyCodeEnt.Type = “anim”

if SERVER then
function MyCodeEnt:Initialize()
self:SetModel(“models/props_c17/concrete_barrier001a.mdl”)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
end
function MyCodeEnt:StartTouch(ent)
if ent:IsPlayer() then
ent:ChatPrint(“I Touched You!!”)
end
end
else

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

end
scripted_ents.Register(MyCodeEnt, “code_ent”, true)
[/lua]

Hope this helps, that code has been tested and works.

[editline]1st June 2011[/editline]

oh yea and the second arg in the scripted_ents.Register is the entities name, so to spawn the test it’s like
ents.Create(“code_ent”)

I personally used ShouldCollide, but it’s not a dedicated hook for this. It’s a little more complicated, but at least you don’t have to create an entire new entity.

Thanks, Also

:ohdear:

[editline]2nd June 2011[/editline]

Oh wonderful, I mixed it in with my code and it now says that when you did it

Though i changed it to ban them.

Updated OP with code that just says I Touched You!!! when hit with the train!

I will update it again once I get the banning feature working!

[editline]2nd June 2011[/editline]

Also, If you want to edit my code to work with evolve’s banning system, I’m more then happy!