How to Spawn Vehicles in Lua

I can’t figure out how to spawn vehicles in lua, i have looked around and found a similar thread with this code:

	local trace = { } 
	trace.start = ply:EyePos()  
	trace.endpos = trace.start + ply:GetAimVector() * 85  
	trace.filter = ply  
 
	local car = ents.Create("prop_vehicle_jeep_old")  
	car:SetModel("models/buggy.mdl") 
	car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")
	car:SetPos(tr.HitPos) 
	car:Spawn()

When I assign this to a button however i get the following error message:

           gamemode/init.lua:66: attempt to index global 'tr' (a nil value)

I think it means it cant get the position that the player is looking but I have no idea how to fix it. Its probably something obvious so I am sorry, but any help will be greatly appreciated.

Edit.

I Changed the code to:

	local car = ents.Create("prop_vehicle_jeep_old")  
	car:SetModel("models/buggy.mdl") 
	car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")
	car:Spawn()

Now the vehicles spawn but only if sv_cheats 1 is on. I want to be able to spawn a vehicle without the need for sv_cheats to be 1. Again, thanks for any help.

The problem with your first code is the name of the trace is “trace”, not tr.

i changed to car:SetPos(tr.HitPos) to car:SetPos(trace.HitPos) but i still get the nil value error. Also, do you have any idea how to make it work without sv_cheats being 1 ?

Edit.

The error now is:

gamemode/cl_init.lua:89: attempt to index global ‘ply’ (a nil value)

Well, you don’t have a ply. This needs to be a function of sorts. Establish who the player is.

I’m guessing that you can only spawn vehicles from console from the client if sv_cheats = 1; you can probably do without it on the server.

Also, remember to use the [ lua] [/lua] tags (without the space) to make it easier for people to read your code.

Ok thanks, I will have a go at changing some stuff, thanks again for the help.

Edit.

I can’t figure out how to do this, how would you write a function to establish who the player is?

It depends. What’s the situation?

Well, when you press f3 a button comes up, which when you click it runs a console command <=== in cl_init

The command activates a function which is the code above <== in init.lua

Is that what you mean? Sorry if its not.

OK. In that case, ply is correct. I only just noticed, you need to do local tr = util.TraceLine( trace ). Put that in the original script.

[lua]local trace = { }
trace.start = ply:EyePos()
trace.endpos = trace.start + ply:GetAimVector() * 85
trace.filter = ply

local tr = util.TraceLine( trace )

local car = ents.Create(“prop_vehicle_jeep_old”)
car:SetModel(“models/buggy.mdl”)
car:SetKeyValue(“vehiclescript”,“scripts/vehicles/jeep_test.txt”)
car:SetPos(tr.HitPos)
car:Spawn()[/lua]

Ok i added that line, but still i get the nil value error. Here is what I have:

cl_init.lua
[lua] Make.DoClick = function()
ply:ConCommand( “SpawnJeep” )
end
[/lua]

init.lua
[lua]function SpawnJeep( ply )

local trace = { }  
trace.start = ply:EyePos()  
trace.endpos = trace.start + ply:GetAimVector() * 85  
trace.filter = ply  
 
local tr = util.TraceLine( trace )  
  
local car = ents.Create("prop_vehicle_jeep_old")  
car:SetModel("models/buggy.mdl")  
car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")  
car:SetPos(tr.HitPos)  
car:Spawn() 

end
[/lua]

And the error code once again:
gamemode/cl_init.lua:87: attempt to index global ‘ply’

My steam account name is Shifty15, feel free to add me, since replies on the forum can take a while.

In client side files (e.g. cl_init) the player is LocalPlayer()

You can store it in a variable if you want though: local ply = LocalPlayer();
note you only need to do that if you use LocalPlayer() a lot as instead of lots of function calls it will refer to the variable instead.

Ok I changed the cl_init.lua to this:

[lua] Make.DoClick = function()
local ply = LocalPlayer()
ply:ConCommand( “SpawnJeep” )
end

[/lua]

And now the error message has gone away. Only now it says "unknown command ‘SpawnJeep’. I have remembered to put
[lua]concommand.Add( “SpawnJeep”, SpawnJeep )[/lua]
underneath it so I don’t know what it means by unknown command.

Thanks alot though for everyones help so far.

Can you post the full init.lua?

Uh, could it be possible for you to tell me what to look for, I’d rather not post the init.lua on the forums. (my own reasons). However, if it is the only way to fix it i will. You could also add me on steam and discuss it further over chat if you want. Sorry.

Edit.

Steam account: Shifty15

Edit.

I just noticed, when I start the server it says “shared.lua File not Found” or something along those lines, but my teams and the like are still working which are set up in the shared.lua file. I don’t know if this makes a difference but I thought I should give all the infomation I have.