spawn entity on spawn

Hello.

What code can i use to spawn a entity right in front of the player when he/she spawns?

Din’t tested it, but maybe this should work

[lua]
function GM:PlayerSpawn ( ply )
self.BaseClass:PlayerSpawn ( ply )

trace = ply.GetPos()
ent = ents.Create ( "entity here" )
ent:SetPos(trace.HitPos)
ent:Spawn()

end
[/lua]

edit: Had to change code

Ok. Thank you, ill have a look and see if there are any errors and attempt to fix them

[editline]06:17PM[/editline]

Hmm i don’t know if it’s your script or my problem. See i want it to spawn the parachute addon seen here http://www.garrysmod.org/downloads/?a=view&id=35133 . it says the entities called sent_chute thats what i typed but no luck with it. Any more help?

[lua]function GM:PlayerSpawn(ply)
local ent = ents.Create(“sent_chute”)
ent:SetPos(ply:GetPos() + ply:GetAimVector()*16)
ent:Spawn()
return self.BaseClass.PlayerSpawn(self, ply)
end[/lua]

Attempt 2 here we go.

[editline]07:58PM[/editline]

hmm second fail but let me try something

Don’t you need to activate the entity by doing ent:Activate()? Just a shot in the dark.

Nope epic fail!! i wonder what could be going wrong?

[editline]08:13PM[/editline]

sorry that wasn’t @decyg it was at me for failing

[editline]08:15PM[/editline]

so. Is this code telling it to spawn at my position? Or at where my mouse is pointing.

[editline]08:28PM[/editline]

would this have something to do with it?



function ENT:SpawnFunction( ply, tr )
    if ( !tr.Hit ) then return end
     local SpawnPos = tr.HitPos + tr.HitNormal * 16
     local ent = ents.Create( "sent_ball" )
    ent:SetPos( SpawnPos ) 
    ent:Spawn()
    ent:Activate()
end

Got that off hte gmod wiki. It states it spawns at where the player is looking but deosn’t say at spawn or anything

No, that’s just how the Bouncy Ball is spawned, when you press the spawnicon. Try changing the function name to “PlayerSpawn” instead of the ENT:Spawn thingi.

Yeah

[editline]09:41PM[/editline]

I may aswell try now

Ok i got somewhere i made it spawn where i am with this code


 function GM:PlayerSpawn( ply )
local ent = ents.Create( "sent_chute" )
ent:SetPos( ply:GetPos() ) 
ent:Spawn()
ent:Activate()
end


It works fine. But spawns right where i am. Can i use like subtract on the vectors so it spawns maybe a meter infront of me?

Anyways thanks if you can help

yes, you can use mathematics on vectors, like this[lua] function GM:PlayerSpawn( ply )
local ent = ents.Create( “sent_chute” )
local Pos = ply:GetPos()
ent:SetPos( Vector(Pos.x + 20, Pos.y + 20, Pos.z + 30) )
ent:Spawn()
ent:Activate()
end[/lua]



function GM:PlayerSpawn( ply )
local ent = ents.Create( "sent_chute" )
local Pos = ply:GetPos()
ent:SetPos( Vector(ply:GetPos.x) + 20, (ply:GetPos.y) + 20,(ply:GetPos.z) + 30 )
ent:Spawn()
ent:Activate()
end

would that work?

anyone?

Try this but change the co ords as you see fit.

[lua] function GM:PlayerSpawn( ply )
local ent = ents.Create( “sent_chute” )
local Pos = ply:GetPos()
ent:SetPos( pos + Vector(0, 0, 50) )
ent:Spawn()
ent:Activate()
end[/lua]

-snip-

will the code i made work? what i wanted it to do was find the player pos.x player pos.y player pos.z then add a number on it to spawn the entity infront of the player. Sorry for that spelling.

Test it, then you will know.

I sure will mate

[lua]function GM:PlayerSpawn( ply )
local ent = ents.Create( “sent_chute” )
ent:SetPos( ply:GetPos() + ply:GetAngles():Forward() * 64 + Vector( 0, 0, 16 ) )
ent:Spawn()
ent:Activate()
end[/lua]

Spawns the entity 64 units in front of the player and 16 units above the ground.

Oh thankyou! this was what i was trying to do but just in my own noob way!