General > Developers Corner

Vehicle restricting

<< < (2/3) > >>

ahref:
i tired this:


--- Code: ---local function spawned_vehicle( ply, prop )
if ( SinglePlayer() ) then return true end
if ply:IsAdmin() or ply:IsSuperAdmin() then
return true
end
if ( prop:GetClass( ) != "prop_vehicle_prisoner_pod" ) then
ply:PrintMessage( HUD_PRINTTALK, "This is Garrys Mod BUILD A VEHICLE(seats and pods are still allowed)" ) 
return false
else
return true
end
end

function GAMEMODE:PlayerSpawnVehicle( ply, prop )

return spawned_vehicle( ply, prop )

end

--- End code ---

as it seems from the example that it returns an entity not a propid :S

JamminR:
Any errors in server console when non-admin spawns a vehicle?
Tried printing to your HUD or console what "prop" actually contains?
That line of troubleshooting would help you quickly ascertain how to go about checking it, whether it actually be a model, entity, or class.

Also - Tip for Lua best practice -

--- Code: ---if ( prop:GetClass( ) != "prop_vehicle_prisoner_pod" ) then

--- End code ---
Garry added several of his own programmng contructs to what I call 'G-Lua'. (Gmod Lua)
One of those is the "not equal" construct.
Though in Gmod your "!=" should work, best practice would be to not follow Garry's lead there.
I recommend using
--- Code: ---if not prop:GetClass( ) == "prop_vehicle_prisoner_pod" then
--- End code ---
That makes it much easier to tell when debugging A should not equal B
There IS an 'abbreviated' way, but I find it's not as easy to see. The true not equal abbreviation in Lua is "~="
As you can see, that would be much more difficult to notice than Garry's "!=", and could easily be lost while looking at code

JamminR:
http://wiki.garrysmod.com/wiki/?title=Gamemode.PlayerSpawnedVehicle
Try adding a hook rather than overwriting the Gamemode function.
You would need to change your "return true" lines to just "return"
"Return false" would remain the same

ahref:
@G-Lua commens, Yeah that is better :P

@Hook, Would that still be able to deny the spawning of the vehicle?

JamminR:
Yes.
Returning _anything_, I believe even including true, stops the Gmod function for spawning it.
So, that's why you'd remove the 'true' also
(Others, feel free to correct me on this)


--- Code: ---local function spawned_vehicle( ply, prop )
   if ( SinglePlayer() ) then return end
   if ply:IsAdmin() or ply:IsSuperAdmin() then
      return
   end
   if not prop:GetClass( ) == "prop_vehicle_prisoner_pod" then
      ply:PrintMessage( HUD_PRINTTALK, "This is Garrys Mod BUILD A VEHICLE (seats and pods are still allowed)" )
      return false
   else
      return
   end
end

hook.Add( "PlayerSpawnedVehicle", "CheckIfCar", spawned_vehicle )

--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version