In this case I would use Humanoid.MoveDirection which should allow you to find the direction a player is moving in. After this you can determine what direction should correlate to what walkspeed.
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
KeyTable.Left = true
if KeyTable.Backward == true then return end
if KeyTable.Forward == true then return end
Humanoid.WalkSpeed = SideSpeed
elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
KeyTable.Right = true
if KeyTable.Backward == true then return end
if KeyTable.Forward == true then return end
Humanoid.WalkSpeed = SideSpeed
elseif UserInputService:IsKeyDown(Enum.KeyCode.S) then
KeyTable.Backward = true
Humanoid.WalkSpeed = BackSpeed
elseif UserInputService:IsKeyDown(Enum.KeyCode.W) then
KeyTable.Forward = true
Humanoid.WalkSpeed = NormalSpeed
else
KeyTable.Backward = false
KeyTable.Forward = false
KeyTable.Left = false
KeyTable.Right = false
end
Another thing to keep in mind for the future of your project, if you are going to add/have added an anticheat which includes walk speed detections I would recommend adding enough leniency to prevent false positives because of walk speed changes in regard to this system.
You can incorporate this into your sprint mechanic in a few ways.
For one, you could do something slightly different and instead of directly changing walk speed, you could average out the speed based on if the player is sprinting or not along with the direction.
Another way you could do this is by simply disabling sprinting if the player is not moving forward.
Its not that, If i try sprinting by pressing shift, Since it sets the walkspeed to the normal speed on W it will simply not allow me to go faster then that
I see. You can change the forward speed number to a different one when sprinting along with updating Humanoid.WalkSpeed, which should solve your issue. (Assuming youâre going for the second option)
If youâre trying for the first, what I meant by averaging the speed was doing some simple equations to determine what the speed should be when setting it in your InputBegan connection as well as in your sprint function.