Sprint script issue

What do you want to achieve?
I made a Sprint script but when i’m testing it in play mode, if i don’t move and i only press “Left Shift” running animation will start playing… I want that the running animation only plays when i’m moving, not when i’m standing still.

What solutions have you tried so far?
Tried to find anything useful in Devforum, but couldn’t find anything that worked for me…

I would really appreciate if you could help me out!

Code:

local player = game.Players.LocalPlayer
local char = script.Parent

repeat task.wait() until char ~= nil

local hum = char:WaitForChild("Humanoid")
local NormalRunAnim = hum:LoadAnimation(script:WaitForChild("Run"))

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")

local Remotes = RS:WaitForChild("Remotes")
local RunRemote = Remotes:WaitForChild("Run")

local DefaultFOV = 70
local walkingSpeed = 16
local runningSpeed = 30

local isRunning = player:WaitForChild("isRunning")

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunRemote:FireServer("Running")
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunRemote:FireServer("Walking")
	end
end)
local player = game.Players.LocalPlayer
local char = script.Parent

repeat task.wait() until char ~= nil

local hum = char:WaitForChild("Humanoid")
local NormalRunAnim = hum:LoadAnimation(script:WaitForChild("Run"))

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")

local Remotes = RS:WaitForChild("Remotes")
local RunRemote = Remotes:WaitForChild("Run")

local DefaultFOV = 70
local walkingSpeed = 16
local runningSpeed = 30

local isRunning = player:WaitForChild("isRunning")

UIS.InputBegan:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if hum.MoveDirection ~= Vector3.new(0, 0, 0) then 
			RunRemote:FireServer("Running")
		end
	end
end)

UIS.InputEnded:Connect(function(input,gameprocessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		RunRemote:FireServer("Walking")
	end
end)

Isn’t that a loop? I heard that loops aren’t good for this type of scripts…

i only added:

if hum.MoveDirection ~= Vector3.new(0, 0, 0) then 

which isnt a loop, it just checks if the Humanoids MoveDirection property isnt 0 when Left Shift is pressed, so if the player isnt moving it will just not fire the RemoteEvent

I don’t see any loop there, also you don’t need to fire a RemoteEvent just to change the walkspeed, you can do it all in a LocalScript

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local cam = workspace.CurrentCamera

local properties = {
    WalkSpeed = 16;
    RunSpeed = 20;
    WalkFOV = 70;
    RunFOV = 50;
}

UIS.InputBegan:Connect(function(Input,GPE)
    if GPE then return end

    if Input.Keycode == Enum.Keycode.LeftShift then
        hum.WalkSpeed = properties.RunSpeed
        cam.FieldOfView = properties.RunFOV
    end
end)

UIS.InputEnded:Connect(function(Input,GPE)
    if GPE then return end

    if Input.Keycode == Enum.Keycode.LeftShift then
        hum.WalkSpeed = properties.WalkSpeed
        cam.FieldOfView = properties.WalkFOV
    end
end)

Oh i see, then i will try doing that, Thanks!

Yup i know, that’s not the entire script, i have another section where i need to fire a RemoteEvent

Ok so… seems like adding if hum.MoveDirection ~= Vector3.new(0, 0, 0) then won’t fix the issue i have, when i press Left Shift animation plays when i’m standing still.

hum:GetPropertyChangedSignal("MoveDirection"):Connect(function() 

end)

You can try use this before the InputBegan event.

Seems like that one won’t work either… was thinking on using this, but i think that it’ll create a loop and it won’t be good…

UIS.InputBegan:Connect(function(input,gameprocessed)
    if input.KeyCode == Enum.KeyCode.LeftShift and (hum.MoveDirection.Magnitude > 0) then
        RunRemote:FireServer("Running")
    end
end)