• Attempt to index global 'input' HELP!
    9 replies, posted
Hey there, making a short story long, I made a script which gives me an error while entering a vehicle. I can't figure out how to fix it. :( [CODE] function VEHSTARTUP_ControlVehicle( ply, key, Think ) if ply:InVehicle() and ply:GetVehicle():GetClass() == "prop_vehicle_jeep" then local Vehicle = ply:GetVehicle() local Speed = math.Round( ( ( Vehicle:OBBCenter() - Vehicle:GetVelocity() ):Length() / 17.6 ) / 2 ) if input.IsKeyDown( KEY_I ) then ply.EngineDelay = ply.EngineDelay or 0 if ply.EngineDelay + DMOD_EngineStartupTime > CurTime() then return end ply.EngineTime = CurTime() if Vehicle.EngineStarting then return end if Vehicle.EngineOn then if DMOD_TurnEngineOffWhileDriving then if Speed <= 3 then ply:SendLua("GAMEMODE:AddNotify(\"Engine has been turned off!\", NOTIFY_GENERIC, 3)") Vehicle:Fire("turnoff", "", 0) Vehicle.EngineOn = false else ply:SendLua("GAMEMODE:AddNotify(\"You have to stop the vehicle before turning the engine off!\", NOTIFY_GENERIC, 3)") end else ply:SendLua("GAMEMODE:AddNotify(\"Engine has been turned off!\", NOTIFY_GENERIC, 3)") Vehicle:Fire("turnoff", "", 0) Vehicle.EngineOn = false end else ply:SendLua("GAMEMODE:AddNotify(\"Engine has been turned on!\", NOTIFY_GENERIC, 3)") umsg.Start("VEHSTARTUP_EngineCL", ply) umsg.End() local VehicleSound = ply:GetVehicle():EmitSound( "vehicles/startup.mp3", 75, 100, 1.0) Vehicle.EngineStarting = true timer.Simple( 3, function() if IsValid( Vehicle ) then Vehicle:Fire( "turnon", "", 0 ) Vehicle.EngineOn = true Vehicle.EngineStarting = false end end) end end end end hook.Add( "KeyPress", "VEHSTARTUP_ControlVehicle", VEHSTARTUP_ControlVehicle) function VEHSTARTUP_PlyEnteredCar( ply, Vehicle ) if DMOD_EnterVehicleNotifyON then ply:SendLua("GAMEMODE:AddNotify( DMOD_OnEnterVehicleNotify, NOTIFY_GENERIC, 3)") end if not DMOD_KeepVehicleEngineRunning then Vehicle:Fire("turnoff", "", 0) elseif not Vehicle.EngineOn then Vehicle:Fire("turnoff", "", 0) end end hook.Add("PlayerEnteredVehicle", "VEHSTARTUP_PlyEnteredCar", VEHSTARTUP_PlyEnteredCar) [/CODE]
So basically, you are saying that I have to create a net string client sided which activates the server sided function?
KeyDown isn't even called unless the player presses a key on the IN_Enums, which the 'I' key isn't one of. It'd be better to use a clientside Think hook and check IsKeyDown first, then check all the vehicle stuff [editline]15th December 2016[/editline] Not sure if this would actually work but just what I mean: [CODE] function VEHSTARTUP_ControlVehicle() if input.IsKeyDown( KEY_I ) then local Vehicle = ply:GetVehicle() if ply:InVehicle() and Vehicle:GetClass() == "prop_vehicle_jeep" then local Speed = math.Round( ( ( Vehicle:OBBCenter() - Vehicle:GetVelocity() ):Length() / 17.6 ) / 2 ) ply.EngineDelay = ply.EngineDelay or 0 if ply.EngineDelay + DMOD_EngineStartupTime > CurTime() then return end ply.EngineTime = CurTime() if Vehicle.EngineStarting then return end if Vehicle.EngineOn then if DMOD_TurnEngineOffWhileDriving then if Speed <= 3 then notification.AddLegacy( "Engine has been turned off!", NOTIFY_GENERIC, 3 ) Vehicle:Fire( "turnoff", "", 0 ) Vehicle.EngineOn = false else notification.AddLegacy( "You have to stop the vehicle before turning the engine off!", NOTIFY_GENERIC, 3 ) end else notification.AddLegacy( "Engine has been turned off!", NOTIFY_GENERIC, 3 ) Vehicle:Fire("turnoff", "", 0) Vehicle.EngineOn = false end else notification.AddLegacy( "Engine has been turned on!", NOTIFY_GENERIC, 3 ) umsg.Start("VEHSTARTUP_EngineCL", ply) umsg.End() Vehicle:EmitSound( "vehicles/startup.mp3", 75, 100, 1.0 ) Vehicle.EngineStarting = true timer.Simple( 3, function() if IsValid( Vehicle ) then Vehicle:Fire( "turnon", "", 0 ) Vehicle.EngineOn = true Vehicle.EngineStarting = false end end) end end end end hook.Add( "Think", "VEHSTARTUP_ControlVehicle", VEHSTARTUP_ControlVehicle) [/CODE] Note that with this approach you have to make a variable when IsKeyDown is checked so it doesn't constantly turn on and off the engine every frame you are holding I Also, yeah, net messages are way better
It keeps giving me "Attempt to global index "input", even with your code. :(` *edit: it gives me now attempt to index local "input".
The input library is still only available clientside, and you're still running it serverside.
Is there an alternative to input.WasKeyPressed then to run it serversided? Because this script has to be run serversided.
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyPressed]Player:KeyPressed[/url] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyDown]Player:KeyDown[/url] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyReleased]Player:KeyReleased[/url]
[QUOTE=NeatNit;51538711][img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyPressed]Player:KeyPressed[/url] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyDown]Player:KeyDown[/url] [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/KeyReleased]Player:KeyReleased[/url][/QUOTE] These do not work for the I key does it?
Oh sorry, no. If you want to use an otherwise unbound key, there's no simple way to get it serverside. The client has to explicitly send it to the server.
Sorry, you need to Log In to post a reply to this thread.