• Garry's Mod Lua Help (Slower Walk Speed)
    14 replies, posted
Hey guys, I'm managing a Murder server right now and I heavily modified the gamemode so it's not the same anymore. As of right now I am trying to modify the movement speed for crouching, slow walking, walking, and sprinting. I managed to edit everything in there to what I want except for the slow walking speed (alt key by default). The lua resource is super confusing because SetWalkSpeed() is not the actual walk speed but the "run" speed. I tried looking around to find the (slow) walk speed but I can't find anything for it. Does anyone know the player lua code to set the slower walk speed?
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/SetPlayerSpeed]GM:SetPlayerSpeed[/url]
[QUOTE=Crimsong;51235313]Hey guys, I'm managing a Murder server right now and I heavily modified the gamemode so it's not the same anymore. As of right now I am trying to modify the movement speed for crouching, slow walking, walking, and sprinting. I managed to edit everything in there to what I want except for the slow walking speed (alt key by default). The lua resource is super confusing because SetWalkSpeed() is not the actual walk speed but the "run" speed. I tried looking around to find the (slow) walk speed but I can't find anything for it. Does anyone know the player lua code to set the slower walk speed?[/QUOTE] You could always look in the gamemode files I'm not at my computer right now but it should be in something like player_class.lua or something like that. If you can't find it in the murder files, look in the base gamemode files.
The only ones I found are SetCrouchWalkSpeed() SetWalkSpeed() SetRunSpeed() and none of those edit the actual slower walk speed (alt key). [editline]20th October 2016[/editline] @VeXan is that "walk" speed for the ALT WALK function?
Maybe you can use [url=http://wiki.garrysmod.com/page/Player/SetCanWalk]Player:SetCanWalk[/url] (only works serverside apparently) and [URL="http://wiki.garrysmod.com/page/input/IsKeyDown"]make your own input handler for ALT[/URL]?
I could do that, but how can I multiply the player's movement speed while the key is being pressed down?
check if the key is down, get the players speed, and set its new speed multiplied by X?
@whitestar I mean by lua code, I'm pretty new to lua. I can get player speed but how do I set player speed?
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/SetWalkSpeed]Player:SetWalkSpeed[/url] [url]http://wiki.garrysmod.com/[/url]
Hmm I figured as much. I'll give it a try when I get home.
Does this look right? [CODE]if input.IsKeyDown( KEY_LALT ) then self:SetWalkSpeed(self:GetWalkSpeed * 1.2) else self:SetWalkSpeed(walk) end[/CODE]
[QUOTE=Crimsong;51237611]Does this look right? [CODE]if input.IsKeyDown( KEY_LALT ) then self:SetWalkSpeed(self:GetWalkSpeed * 1.2) else self:SetWalkSpeed(walk) end[/CODE][/QUOTE] Self won't work since self is not a player. Also, if you keep multiplying speed by 1.2 it will constantly increase. And where are you getting the walk variable from? Clientside has no control over walkspeed so you need to[URL="http://wiki.garrysmod.com/page/Net_Library_Usage"] network it to the server[/URL] and set it there. (Sorry super tired can't help any further tonight. Good luck!)
Ahhh this is more complicated than I thought... Is there no way to just edit the walk speed? :( Like I said, I'm still pretty new to lua so I'm starting to question if I can even do this anymore.
[QUOTE=Crimsong;51240702]Ahhh this is more complicated than I thought... Is there no way to just edit the walk speed? :( Like I said, I'm still pretty new to lua so I'm starting to question if I can even do this anymore.[/QUOTE] You can do it, you're on the right track with the code you wrote. First you need to think about what you're going to hook this to and if there is already a parameter for the player. Second you need to consider what will be in charge of changing the speed, server or client ( hint it's server ) Third you need to consider how you're setting that movement speed and remember that each time you set it, you are also changing the value that is being returned when you get the movement speed again
Here ya go. Thoroughly tested in singleplayer and multiplayer; took one day to make. Make a Lua script file in lua/autorun folder and paste this sucker in it. Report here if there's any errors/misleading sections. You know, if I could ask for something in return, it would be that you study the code carefully and hopefully consider its inner workings and learn something new. Cheers. :) [CODE]-- Custom set walkspeed script -- Created by SupahMarioX with references from Garry's Mod wiki -- Left Alt is, by default, the walk key, so we have to accomodate players who rebinded their walk key -- to still be able to use this using the IN_WALK enumeration. -- We'll also add a server ConVar to be able to enable and disable the function of the script. --------------------------------------------------------------- ------------ So how the fuck do I use this script? ------------ --------------------------------------------------------------- --------------- Player:SetAltWalkSpeed( <VALUE> ) ------------- ------------------ Set actual walk speed ---------------------- --------------------------------------------------------------- ----------- Player:SetAltCrouchedWalkSpeed( <VALUE> ) --------- --------------- Set crouched custom walk speed ---------------- --------------------------------------------------------------- --- Player:SetAltPlayerSpeed( <WALK_VALUE>, <CROUCH_VALUE> ) -- ------ Basically does above functions but in one function ----- --------------------------------------------------------------- -- ConVars CreateConVar( "sv_walkspeed_enabled", "1", FCVAR_REPLICATED, "Enable custom walk speed control" ) CreateConVar( "sv_walkspeed_restrict_stand", "1", FCVAR_REPLICATED, "Clamp custom stand walk speed value to normal walk speed" ) CreateConVar( "sv_walkspeed_restrict_crouch", "1", FCVAR_REPLICATED, "Clamp custom crouch walk speed value to normal walk speed" ) CreateConVar( "sv_walkspeed_stand_init_val", "100", FCVAR_REPLICATED, "Initial custom stand walk speed value set for each player on spawn" ) CreateConVar( "sv_walkspeed_crouch_init_val", "30", FCVAR_REPLICATED, "Initial custom crouch walk speed value set for each player on spawn" ) -- Default values (based on Sandbox gamemode); change if needed local walkSpeed, runSpeed, crouchSpeed = 200, 400, 60 -- Get player metatable local ply = FindMetaTable( "Player" ) -- Setting custom STAND walk speed function ply:SetAltWalkSpeed( value ) if SERVER then -- Ensure that custom stand walk speed doesn't exceed normal walk speed to accidentally increase speed, unless disabled by ConVar self._WalkSpeed = GetConVar( "sv_walkspeed_restrict_stand" ):GetBool() and value > ( self:GetWalkSpeed() or walkSpeed ) and ( self:GetWalkSpeed() or walkSpeed ) or value net.Start( "CustomPlayerWalkSpeed_NW_UpdateWalkSpeed" ) net.WriteFloat( self._WalkSpeed ) net.Send( self ) elseif CLIENT then -- We say fuck-you to the client because the speed should be altered serverside end end -- Setting custom CROUCH walk speed function ply:SetAltCrouchedWalkSpeed( value ) if SERVER then -- Ensure that custom crouch walk speed doesn't exceed normal crouch walk speed to accidentally increase speed, unless disabled by ConVar self._CrouchWalkSpeed = GetConVar( "sv_walkspeed_restrict_crouch" ):GetBool() and value > crouchSpeed and crouchSpeed or value net.Start( "CustomPlayerWalkSpeed_NW_UpdateCrouchSpeed" ) net.WriteFloat( self._CrouchWalkSpeed ) net.Send( self ) elseif CLIENT then -- meh, you get it end end -- Setting custom player speed; convenience function I guess function ply:SetAltPlayerSpeed( walk, crouch ) self:SetAltWalkSpeed( walk ) self:SetAltCrouchedWalkSpeed( crouch ) end -- Server stuff here if SERVER then -- durr util.AddNetworkString( "CustomPlayerWalkSpeed_NW_UpdateWalkSpeed" ) util.AddNetworkString( "CustomPlayerWalkSpeed_NW_UpdateCrouchSpeed" ) util.AddNetworkString( "CustomPlayerWalkSpeed_NW_PlayerSpawn" ) -- Initialize custom walkspeed variable hook.Add( "PlayerSpawn", "CustomPlayerWalkSpeed_SV_PlayerSpawn", function( ply ) local walk, crouch = GetConVar( "sv_walkspeed_stand_init_val" ):GetFloat(), GetConVar( "sv_walkspeed_crouch_init_val" ):GetFloat() -- Set individual player's custom walk/crouch speed ply:SetAltPlayerSpeed( walk, crouch ) -- Current workaround for unknown error in initializing clientside speed variables on multiplayer timer.Simple( 1, function() net.Start( "CustomPlayerWalkSpeed_NW_PlayerSpawn" ) net.WriteFloat( walk ) net.WriteFloat( crouch ) net.Send( ply ) end ) end ) end -- Client stuff here if CLIENT then -- Receive serverside player custom stand walk speed packet net.Receive( "CustomPlayerWalkSpeed_NW_UpdateWalkSpeed", function( len ) LocalPlayer()._WalkSpeed = net.ReadFloat() end ) -- Receive serverside player custom crouch walk speed packet net.Receive( "CustomPlayerWalkSpeed_NW_UpdateCrouchSpeed", function( len ) LocalPlayer()._CrouchWalkSpeed = net.ReadFloat() end ) -- Receive initialization packet net.Receive( "CustomPlayerWalkSpeed_NW_PlayerSpawn", function( len ) local ply = LocalPlayer() if not ply._WalkSpeed and not ply._CrouchWalkSpeed then ply._WalkSpeed = net.ReadFloat() ply._CrouchWalkSpeed = net.ReadFloat() end end ) end -- Setup the custom walk speed control in SetupMove hook.Add( "SetupMove", "CustomPlayerWalkSpeed_SH_SetupMove", function( ply, mv, cmd ) -- ConVar boolean check if not GetConVar( "sv_walkspeed_enabled" ):GetBool() then return end -- If walk key is down and not holding the sprint key at the same time if mv:KeyDown( IN_WALK ) and not mv:KeyDown( IN_SPEED ) then -- We should consider the speed of ducking/crouching while walking if mv:KeyDown( IN_DUCK ) then mv:SetMaxClientSpeed( ply._CrouchWalkSpeed ) else mv:SetMaxClientSpeed( ply._WalkSpeed ) end end end ) -- Actually perform the action when holding down the walk button hook.Add( "Move", "CustomPlayerWalkSpeed_SH_Move", function( ply, mv ) if not GetConVar( "sv_walkspeed_enabled" ):GetBool() then return end if mv:KeyDown( IN_WALK ) and not mv:KeyDown( IN_SPEED ) then -- We should consider the speed of ducking/crouching while walking if mv:KeyDown( IN_DUCK ) then mv:SetMaxSpeed( ply._CrouchWalkSpeed ) else mv:SetMaxSpeed( ply._WalkSpeed ) mv:SetMaxClientSpeed( ply._WalkSpeed ) end end end )[/CODE]
Sorry, you need to Log In to post a reply to this thread.