Hey guys, today I decided to make a relatively simple movement system for my game, but I’m currently stuck on how to achieve a goal. Whenever the player is running (activated by holding down the left shift key), changing directions or moving the camera causes them to return to their walking speed. I tried tracking the appropriate key binds in my script but that hasn’t yielded any success for me. Below will be the current ModuleScript, any help given trying to overcome this quirk will be appreciated as usual :^D
local runningModule = {}
--Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--Values
local canRun = ReplicatedStorage.Values.BoolValues.PlayerMovement.canRun
--Variables
local clientVariables = require(ReplicatedStorage.Modules.Data.clientVariables)
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local running = UserInputService.InputBegan:Connect(function()
return
end)
local stopping = UserInputService.InputEnded:Connect(function()
return
end)
local isRunning = false
--Dictionary
local WASDControls = {
[Enum.KeyCode.W] = false,
[Enum.KeyCode.A] = false,
[Enum.KeyCode.S] = false,
[Enum.KeyCode.D] = false,
[Enum.KeyCode.Thumbstick1] = false,
}
--Running input
local function run(input)
return (input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.ButtonA)
end
--Check for WASD controls
local function areWASDControlsReleased()
return (
WASDControls[Enum.KeyCode.W] == false
and WASDControls[Enum.KeyCode.A] == false
and WASDControls[Enum.KeyCode.S] == false
and WASDControls[Enum.KeyCode.D] == false
and WASDControls[Enum.KeyCode.Thumbstick1] == false
)
end
--Start and stop running
local function startRunning()
isRunning = true
humanoid.WalkSpeed = clientVariables.runSpeed
end
local function stopRunning()
isRunning = false
humanoid.WalkSpeed = clientVariables.walkSpeed
end
--Enable and disable running, PC/Console
local function enableRunning()
running = UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if isRunning == false and run(input) then
startRunning()
end
end)
stopping = UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if isRunning == true and areWASDControlsReleased() then
stopRunning()
end
end)
end
local function disableRunning()
humanoid.WalkSpeed = clientVariables.walkSpeed
running:Disconnect()
stopping:Disconnect()
running = nil
stopping = nil
end
function runningModule.canPlayerRun()
if canRun.Value == true then
enableRunning()
else
disableRunning()
end
end
return runningModule