A way to stop players from moving a car forward & backward?
6 replies, posted
Hello, I've been devising a fuel system for vehicles on my Dark RP server that, of course, would ideally disable the vehicle from moving forwards & back when the player hits their forward and back keys, but at the same time allow them to steer the wheels (left/right keys). I've run out of ideas however, i've tried listening to the player's key down/key up hooks and locking them, i've tried messing with the vehicle's physics, i've tried many other things and to no avail.
Does anybody here have any idea how to properly do this?
Why? Why would you do this?!
Because if a car runs out of gas, the engine usually dies? I'm assuming that is what he's trying to do.
[QUOTE=Gulen;39899678]Because if a car runs out of gas, the engine usually dies? I'm assuming that is what he's trying to do.[/QUOTE]
Yes. The idea is that once their car's gas variable hits 0, the car will still react to regular physical interactions, but won't go anywhere if they press forward or back, but would allow them to steer the car.
[QUOTE=CrAzY991;39899701]Yes. The idea is that once their car's gas variable hits 0, the car will still react to regular physical interactions, but won't go anywhere if they press forward or back, but would allow them to steer the car.[/QUOTE]
Ohh..
I spent about 40 minutes testing all the possibilities for this type of request. I tested key pressing, and used the KeyPress and KeyRelease hook.
The issue with stopping a KeyPress in a KeyPress hook with the opposite command is that if you use this players are still able to move their vehicle forward but only slightly.
[B]Do NOT use this code at any time for this purpose.[/B]
[lua]
hook.Add("KeyPress", "CheckCarFuel", function(ply, key)
if !IsFirstTimePredicted() then return end
if !IsValid(ply) or ply != LocalPlayer() then return end
if ply:InVehicle() and ply:GetVehicle().Gas and ply:GetVehicle().Gas == 0 then
if key == IN_FORWARD then
RunConsoleCommand("-forward")
elseif key == IN_BACK then
RunConsoleCommand("-back")
end
end
end)
[/lua]
The issue with the PlayerBindPress hook is that even though you block it by returning true, a player can still use "+forward" while in a vehicle with no gas by entering it into the console and the he will still move forward while in his vehicle.
[B]Do NOT use this code at any time for this purpose.[/B]
[lua]
hook.Add("PlayerBindPress", "CheckCarGas", function(ply, bind, pressed)
if !IsValid(ply) then return end
if bind == "+forward" or bind == "+back" then
if !ply:InVehicle() then
print("Not in a vehicle")
return false
elseif ply:GetVehicle().Gas != 0 then
print(ply:GetVehicle().Gas)
print("Gas is perfectly fine")
return false
end
print("Out of Gas")
return true
end
end)
[/lua]
This is the best conclusion from what I have tested and is the most effective.
You want to put this at a point when the car runs out of gas and this will only have to be ran once.
[B]This is so far the best solution.[/B]
[lua]constraint.Weld(ent,game.GetWorld(),0,0,0,true)[/lua]
As a result of this, I hope to that you NEVER use a Think hook for your fuel system because it's not necessary in any of your cases since you can just use the KeyPress and KeyRelease hooks to remove or add fuel to the vehicle.
Use
[lua]ent:Fire("TurnOff", "",0);[/lua]
Sorry, you need to Log In to post a reply to this thread.