Author Topic: Vehicle restricting  (Read 8276 times)

0 Members and 1 Guest are viewing this topic.

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Vehicle restricting
« on: April 03, 2009, 09:55:32 AM »
Im fed up with people coming on my server to drive around in a car ITS FLAT GRASS. so i want to restrict vehicles to only the prisoner pod so that none of the others can be spawned unless the player is an admin of course.

heres my current code and its not working:

Code: [Select]
function GAMEMODE:PlayerSpawnVehicle( ply, prop )

return spawned_vehicle( ply, prop )

end

function spawned_vehicle( ply, prop )
if ( SinglePlayer() ) then return true end
if ply:IsAdmin() or ply:IsSuperAdmin() then
return true
end
if ( prop != "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

what am i dont wrong?

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #1 on: April 03, 2009, 02:45:49 PM »
What isn't working? Non admins still allowed to spawn vehicles?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #2 on: April 03, 2009, 02:48:14 PM »
yes

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #3 on: April 03, 2009, 03:28:52 PM »
Few quick theories; test at your own pace. :P

I poked around in Garry's code by looking in gmod/gamemodes/sandbox .. player.lua and command.lua. (He REALLY needs to update code.garrysmod.com - it's quite out of date.
First;
Isn't prop_blah_blah an entity class, not a model? The references in his code all seem like it would be a model.
(Still seems that should return false though)

Second;
It's been my experience in Lua to place function BEFORE the call to the function. You currently have it backwards.
No idea if that's required for it to work, but it's definitely good practice.

Third;
Tried making functions local? See UNoLimited as an example. (I'm not 100% sure why Megiddo made them local, but I saw that it was, kept it that way, and it worked)
Also, part of this theory...anytime you attempt to spawn a prop, Gmod tries to check to see if the sbox_ limits are set for it... it's possible that function is getting called before yours is.

Fourth...
You're forcing this to run on the server and not the client, right?

Someone else could help here I'm sure.
I'm gonna go play some Fallout 3 now. Had no idea a "choose your own adventure" book (in theory) had been turned into a game.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #4 on: April 03, 2009, 04:08:02 PM »
yes i read unolimited to see how it worked. It also had an init function with nothing in it:

Code: [Select]
local function init()

and later on

hook.Add( "Initialize", "LimitInitilize", init )

@first - i can try models :D
@second - oo thats my fault i had a standard hook in there before.
@third - will try
@fourth - its in my server autorun :D

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #5 on: April 04, 2009, 07:44:34 AM »
i tired this:

Code: [Select]
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

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

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #6 on: April 04, 2009, 10:07:56 AM »
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: [Select]
if ( prop:GetClass( ) != "prop_vehicle_prisoner_pod" ) then
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: [Select]
if not prop:GetClass( ) == "prop_vehicle_prisoner_pod" thenThat 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
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #7 on: April 04, 2009, 10:13:17 AM »
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
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #8 on: April 04, 2009, 10:15:06 AM »
@G-Lua commens, Yeah that is better :P

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

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #9 on: April 04, 2009, 10:19:17 AM »
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: [Select]
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 )
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #10 on: April 04, 2009, 10:39:09 AM »
found out its returning:

Code: [Select]

Redownloading all lightmaps
Airboat
ahref<UNKNOWN> spawned vehicle models/airboat.mdl
Pod
ahref<UNKNOWN> spawned vehicle models/nova/airboat_seat.mdl
Vehicle
ahref<UNKNOWN> spawned vehicle models/buggy.mdl
Pod
ahref<UNKNOWN> spawned vehicle models/nova/jeep_seat.mdl
Pod
ahref<UNKNOWN> spawned vehicle models/nova/chair_office01.mdl
Pod
ahref<UNKNOWN> spawned vehicle models/nova/chair_office02.mdl
Pod
ahref<UNKNOWN> spawned vehicle models/props_phx/carseat2.mdl

which is really odd. but ohwell makes the code easier.

so it is now:

Code: [Select]
local function spawned_vehicle( ply, prop )
if ( SinglePlayer() ) then return true end
if ply:IsAdmin() or ply:IsSuperAdmin() then
return
end
if not prop:GetClass( ) == "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", "ahrefspawnedvehicle", spawned_vehicle )

qhich doesnt work but ill keep poking
« Last Edit: April 04, 2009, 11:54:37 AM by ahref »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #11 on: April 04, 2009, 12:32:51 PM »
OOps.
Sorry. Hook PropSpawnedVehicle will not work.
From it's description - Bold'ed for clarity
"Called when a player has already spawned a vehicle. Use this to record the vehicle's propid. Useful for prop protection.

See http://wiki.garrysmod.com/wiki/?title=Gamemode.PlayerSpawnProp for better way.
« Last Edit: April 04, 2009, 12:34:50 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #12 on: April 07, 2009, 06:13:00 AM »
now im at this:

Code: [Select]
local function spawned_vehicle( ply, prop, ent )
if ( SinglePlayer() ) then return true end
if ply:IsAdmin() or ply:IsSuperAdmin() then
return
end
if not ent: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( "PlayerSpawnProp", "ahrefspawnedvehicle", spawned_vehicle );

which doesnt work. at this point is probably just me though :(

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Vehicle restricting
« Reply #13 on: April 07, 2009, 06:30:56 PM »
The Gmod wiki article for PlayerSpawnedProp mentions nothing of your 3rd variable "ent" passed to your function.
Did you find code or mention of it somewhere?
Does 'Ent' actually get passed to it?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline ahref

  • Jr. Member
  • **
  • Posts: 62
  • Karma: 4
Re: Vehicle restricting
« Reply #14 on: April 08, 2009, 01:30:06 AM »
sigh. i had a tab open with a hook. that had the 3 inputs must've asumed it was spawnprop. right now to get those model names :D