I have a sprinting system that works fine apart from the problem mentioned, the way it’s supposed to work is while the shift key is being held down it plays and when it stops being held down it stops, a very basic sprinting system.
Here’s the code:
-- Client --
RunService.RenderStepped:Connect(function()
if UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
Running = true
Run:FireServer(Running)
else
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
local pitch, yaw, roll = camera.CFrame:ToEulerAnglesYXZ()
char:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(char.HumanoidRootPart.CFrame.p) * CFrame.Angles(0, yaw, 0)
Running = false
Run:FireServer(Running)
end
end)
-- Server --
local function EnableSprint(player,Running)
if not player then return end
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")
if not hum or not player or not char then return end
local IsRunningVal = player:FindFirstChild("IsSprinting")
local BlockingVal = player:FindFirstChild("Blocking")
local RunningAnim = player:FindFirstChild("Backpack"):FindFirstChild("Combat"):FindFirstChild("Animations"):WaitForChild("Movement"):WaitForChild("Run")
local WalkAnim = player:FindFirstChild("Backpack"):FindFirstChild("Combat"):FindFirstChild("Animations"):WaitForChild("Movement"):WaitForChild("Walk")
local PlayRunAnim = hum:LoadAnimation(RunningAnim)
local PlayWalkAnim = hum:LoadAnimation(WalkAnim)
if Running == true then
if IsRunningVal.Value == false and BlockingVal.Value == false then
IsRunningVal.Value = true
PlayRunAnim:Play()
hum.WalkSpeed = 22
print("sprint")
else
return end
end
if Running == false then
if IsRunningVal.Value == true then
IsRunningVal.Value = false
PlayRunAnim:Stop(0)
hum.WalkSpeed = 13
print("walk")
else
return end
end
end
You can use Humanoid.MoveDirection to set Running to false and stop the animation and adjust the walkspeed of the character. You would want to run this on the server and fire the remote
if Humanoid.MoveDirection == Vector3.new(0,0,0) then
Running = false
PlayRunAnim:Stop()
hum.WalkSpeed = 13
end
Edit: It looks like the character stopped moving really briefly in the video. If that’s not the case, this won’t work.
Sorry for the late response, I was planning on adding some features to go along with sprinting that I might not want the client to be in control so I figured I would just have the client detect the input and let the server handle what I want to happen
try
local UIS = game:GetService(‘UserInputService’)
local Player = game.Players.LocalPlayer
local Character = Player.Character
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 25
local Anim = Instance.new(‘Animation’)
Anim.AnimationId = --id here
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
end
end)
UIS.InputEnded:connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
Character.Humanoid.WalkSpeed = 16
PlayAnim:Stop()
end
end)