I made a sprint script for my game but whenever the player holds shift it plays the running animation and zooms the screen out as if you were running. How do I fix this?
This is the main function for the script:
UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
if not Processed then
if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then
Humanoid.WalkSpeed = Speed
SprintingTween:Play()
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://10741505640'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
PlayAnim:Play()
end
end
end)
You can check the MoveDirection of the humanoid if it’s more than 0 (which means walking) and UserInputService:IsKeyDown() to check if ‘Shift’ is being held.
local UIS = game:GetService("UserInputService")
local PS = game:GetService("Players")
local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
UIS.InputBegan:Connect(function(input, gpe)
if gpe then
return
end
if input.KeyCode == Enum.KeyCode.LeftShift and hum.MoveDirection.Magnitude > 0 then
-- do all running effects
warn("running")
repeat task.wait() until not UIS:IsKeyDown(Enum.KeyCode.LeftShift) or hum.MoveDirection.Magnitude <= 0
-- stop all running effects
warn("not running")
end
end)