Another LUA Question: GM_Spawnvehicles in a gamemode

would this work?
[lua] /---------------------------------------------------------
Name: CCSpawnVehicle
Desc: Player attempts to spawn vehicle
---------------------------------------------------------
/
function CCSpawnVehicle( Player, command, arguments )

 if ( arguments[1] == nil ) then return end   
 if ( !gamemode.Call( "PlayerSpawnVehicle", Player ) ) then return end   
        
 local vname = arguments[1]   
 local VehicleList = list.Get( "Vehicles" )   
 local vehicle = VehicleList[ vname ]   
    
 // Not a valid vehicle to be spawning..   
 if ( !vehicle ) then return end   
    
 local tr = Player:GetEyeTraceNoCursor()   
    
 local Ent = ents.Create( vehicle.Class )   
 if ( !ValidEntity( Ent ) ) then return end   
    
     if ( vehicle.Model ) then   
         Ent:SetModel( vehicle.Model )   
     end   
        
     if ( vehicle.KeyValues ) then   
         for k, v in pairs( vehicle.KeyValues ) do   
             Ent:SetKeyValue( k, v )   
         end        
     end   
        
         // Rotate to face player (expected behaviour)   
     local Angles = Player:GetAngles()   
         Angles.pitch = 0   
         Angles.roll = 0   
         Angles.yaw = Angles.yaw + 180   
     Ent:SetAngles( Angles )   
        
     Ent:SetPos( tr.HitPos )   
        
 Ent:Spawn()   
 Ent:Activate()   
    
 undo.Create( "Vehicle" )   
     undo.SetPlayer( Player )   
     undo.AddEntity( Ent )   
     undo.SetCustomUndoText( "Undone "..vehicle.Name )   
 undo.Finish( "Vehicle ("..tostring( vehicle.Name )..")" )   
    
 Player:AddCleanup( "vehicles", Ent )   
    
 gamemode.Call( "PlayerSpawnedVehicle", Player, Ent )   

end

concommand.Add( “gm_spawnvehicle”, CCSpawnVehicle )
[/lua]
if not,what do you suggest?

I see the sandbox code. Did you modify anything?

First, yuck!! This code had all of it’s tabs replaced by spaces, with some extra ones added seemingly for fun as well as faulty indentation.

Otherwise, yes it would work if you remove [lua]if ( !gamemode.Call( “PlayerSpawnVehicle”, Player ) ) then return end [/lua]
because that hook won’t be called outside of Sandbox. Unless you make your own that is.

Depending of what you want to do you could also remove most of the code in there to leave only the vehicle spawning and setup.

More details will lead to more help.

Thanks!
Also,Is there a way to allow "undo"ing them?

its already added in the code…

     undo.Create( "Vehicle" )   
         undo.SetPlayer( Player )   
         undo.AddEntity( Ent )   
         undo.SetCustomUndoText( "Undone "..vehicle.Name )   
     undo.Finish( "Vehicle ("..tostring( vehicle.Name )..")" )   
        
     Player:AddCleanup( "vehicles", Ent )   

Thank you for your help.

no problem :slight_smile:

EDIT:
yes! my 300th post

It’s like that everywhere in the base code.

It isn’t, code like that would be unmaintainable.

http://luabin.foszor.com/code/gamemodes/sandbox/gamemode/commands.lua#710

Click “Plain View” before copy and pasting next time.