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.