• Changing the Player Movement Speed
    30 replies, posted
Is there any new way to change player run/walk/sprint speed? As of the latest update, I'm finding that .lua attempts to change player speed aren't working. Addons like "Half-Life 2 Settings" don't seem to work either to change the speed. This can be pretty annoying if you're trying to play campaign or do NPC battles and such. function SetSpeed(ply) ply:SetWalkSpeed(150) ply:SetRunSpeed(250) end hook.Add("PlayerSpawn", "SetSpeed", SetSpeed) I tried that code, but that didn't work. Any ideas?
Where are you putting this code?
[QUOTE=mib999;45380035]Where are you putting this code?[/QUOTE] GarrysMod/garrysmod/lua/autorun
The PlayerSpawn hook is a server-side only hook. Place this code in lua/autorun/server/YourFileName.lua
I think it worked, but still sprinting at default Garry's Mod speed. Tried it on both single-player and a self-hosted local server. I recall that a while ago the sprint speed scaled to the other speeds.
Can you verify that the code is actually being ran? Put a print statement and try to find it in the console.
Perhaps it's being overridden by the gamemode's call to [url=http://wiki.garrysmod.com/page/GM/SetPlayerSpeed]SetPlayerSpeed[/url]?
Probably. What would be the correct, full code for the .lua then to set it to default HL2 speed (150, 250)? I'm pretty new to .lua coding and am not sure how to set that up properly.
function SetPlayerSpeed ply:runSpeed(150) ply:sprintSpeed(250) end hook.Add("PlayerSpawn", "SetPlayerSpeed", SetPlayerSpeed) Doesn't work that way.
-edit didn't see original post- What gamemode are you running?
EDIT x2: I'm stupid and put in the wrong movement values. I'm gonna try it with new code This is in Sandbox, in both singleplayer and self-local-hosted servers. This is the code I'm using. [code]function playerSetSpeed(ply) timer.Simple(2, function() GAMEMODE:SetPlayerSpeed(ply, 160, 240) player:SetWalkSpeed( 160 ) player:SetRunSpeed( 240 ) end) end hook.Add( "PlayerSpawn", "playerSetSpeedtest", playerSetSpeed )[/code]
Your code needs to be in lua/autorun/server
[QUOTE=Gmod_Fan77;45394123]EDIT x2: I'm stupid and put in the wrong movement values. I'm gonna try it with new code This is in Sandbox, in both singleplayer and self-local-hosted servers. This is the code I'm using. [code]function playerSetSpeed(ply) timer.Simple(2, function() GAMEMODE:SetPlayerSpeed(ply, 160, 240) player:SetWalkSpeed( 160 ) player:SetRunSpeed( 240 ) end) end hook.Add( "PlayerSpawn", "playerSetSpeedtest", playerSetSpeed )[/code][/QUOTE] Change [B]player[/B] to [B]ply[/B].
Changed player to ply: [code]function playerSetSpeed(ply) timer.Simple(2, function() GAMEMODE:SetPlayerSpeed(ply, 160, 240) ply:SetrunSpeed( 160 ) ply:SetsprintSpeed( 240 ) end) end hook.Add( "PlayerSpawn", "playerSetSpeedtest", playerSetSpeed )[/code] But I still get an error and it isn't working. I've tried both "SetRunSpeed" and "runSpeed" (the latter of which is listed on the .lua's own page), and both give me variants of this error: [code][ERROR] lua/autorun/server/bestmovementspeed.lua:6: attempt to call method 'SetrunSpeed' (a nil value) 1. unknown - lua/autorun/server/bestmovementspeed.lua:6 Timer Failed! [Simple][@lua/autorun/server/bestmovementspeed.lua (line 3)][/code] Is the timer causing this, for some reason? I heard putting a timer function in helped several players make the movement speed coding work.
Lua is case sensitive, make sure it's SetRunSpeed & SetWalkSpeed. I'm not sure if you tried that server-side or if you changed the values then ran it server-side.
[code]function playerSetSpeed(ply) timer.Simple(2, function() GAMEMODE:SetPlayerSpeed(ply, 10, 20) ply:SetWalkSpeed( 10 ) ply:SetRunSpeed( 20 ) end) end hook.Add( "PlayerSpawn", "playerSetSpeedtest", playerSetSpeed )[/code] So I don't get error messages any more, but the speed won't change. Even setting it to stupidly low and noticeable numbers, respawning after loading the game, and trying it in both singleplayer and local; none of it has any effect.
[QUOTE=Gmod_Fan77;45394259][code]function playerSetSpeed(ply) timer.Simple(2, function() GAMEMODE:SetPlayerSpeed(ply, 10, 20) ply:SetWalkSpeed( 10 ) ply:SetRunSpeed( 20 ) end) end hook.Add( "PlayerSpawn", "playerSetSpeedtest", playerSetSpeed )[/code] So I don't get error messages any more, but the speed won't change. Even setting it to stupidly low and noticeable numbers, respawning after loading the game, and trying it in both singleplayer and local; none of it has any effect.[/QUOTE] Just for testing, do this: [code] hook.Add("Think", "OnlyForDebugging", function( ) for k, ply in pairs(player.GetAll()) do ply:SetWalkSpeed( 10 ) ply:SetRunSpeed( 20 ) end end) [/code]
Holy shit, that worked. The speed changed.
[QUOTE=Gmod_Fan77;45394437]As in add that to my current .lua, or make a new one?[/QUOTE] Anywhere, make sure it's serverside.
[QUOTE=AnonTakesOver;45394448]Anywhere, make sure it's serverside.[/QUOTE] EDIT: Yeah, added it to my .lua file and the speed changed accordingly.
Put the code you had before in an addon format so it loads after your Game Mode does.
[QUOTE=Gmod_Fan77;45394437]Holy shit, that worked. The speed changed.[/QUOTE] Yes, but that was for testing, we wouldn't want to use the Think hook for this, do what crazyscouter said. You should try not to ever use the Think hook, I only suggested it because I wanted to see if the code was working.
Gotcha. Only one problem I've noticed this: I was using a mod that enables HL2-style sprint stamina, but with this movement change, my stamina isn't getting depleted. Any ideas why?
[QUOTE=Gmod_Fan77;45394541]Gotcha. Only one problem I've noticed this: I was using a mod that enables HL2-style sprint stamina, but with this movement change, my stamina isn't getting depleted. Any ideas why?[/QUOTE] Show the code?
[QUOTE=AnonTakesOver;45394545]Show the code?[/QUOTE] Not sure on that one. It's in the form of a Workshop addon. [url]http://steamcommunity.com/sharedfiles/filedetails/?id=111335690[/url] And even worse, when I remove your "debugging" code line, the speed reverts to default, even if I put it in addons. It might be a lot to ask, but could you make the code out how it should work, please? I'm really confused on what I'm doing wrong.
[QUOTE=Gmod_Fan77;45394560]Not sure on that one. It's in the form of a Workshop addon. [url]http://steamcommunity.com/sharedfiles/filedetails/?id=111335690[/url] And even worse, when I remove your "debugging" code line, the speed reverts to default, even if I put it in addons. It might be a lot to ask, but could you make the code out how it should work, please? I'm really confused on what I'm doing wrong.[/QUOTE] Umm, there should be a better way then this, but I guess it should work. [code] hook.Add("Think","ForcePlayerSpeed", function() for k,v in pairs(player.GetAll()) do ply:SetWalkSpeed( 10 ) ply:SetRunSpeed( 20 ) end end hook.Add("PlayerSpawn", "ResetPlayerSpeed", function( ply ) ply:SetWalkSpeed( 10 ) ply:SetRunSpeed( 20 ) hook.Remove("Think", "ForcePlayerSpeed") end) [/code] That's a dumb way, but it might work? I'll check the addon.
That code itself didn't work.
[QUOTE=Gmod_Fan77;45394619]That code itself didn't work.[/QUOTE] The problem is that it's getting reset by something, what addons do you have installed? EDIT: That addon is old, December 2012, and has been reported broken numerous times in comments
Quite a few. How do I figure out which one's resetting the speed? I only had one addon that explicitly effected player speed in any way, and that was HL2 Settings. I uninstalled that before I began trying the coding.
Disable them one by one until it works.
Sorry, you need to Log In to post a reply to this thread.