Still running after 'W' key is released

i have a run system in my game which uses the uis so when the leftshift key is pressed the player runs, but for some reason, in my game, everytime the player holds leftshift (only) the run (system & anim) still plays. So i added a humanoid.MoveDirection.Magnitude > 0 thing but still has that error.
Here’s the script :

local UIs = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local localPlayer = Players.LocalPlayer
local Animsfolder = game:GetService("ReplicatedStorage").Anims
local runanim = Animsfolder.RunAnim

local character
local Humanoid
local Ranimtrack

local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local Properties = { FieldOfView = 80 }
local T = game:GetService("TweenService"):Create(Camera, Info, Properties)
local rProperties = { FieldOfView = 70 }
local rInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local rT = game:GetService("TweenService"):Create(Camera, rInfo, rProperties)
--sprintsetting
local BoolValue = false
local Rspeed = 32
local Nspeed = 16
--
local decreasestamina = false
local stamina = 100
--
local startergui = game:GetService("StarterGui")
local staminagui = startergui:WaitForChild("Stamina")
local whitebar = staminagui:WaitForChild("WhiteBar")
local stamcount = whitebar.StaminaCount
local staminanumbervalue = script.Stamina.Value

--

local function Toggle()
	BoolValue = not BoolValue
	if BoolValue then
		T:Play()
		Ranimtrack:Play()
		repeat wait() until not BoolValue
	else
		rT:Play()
		Ranimtrack:Stop()
		repeat wait() until BoolValue
	end
end

local function LoadAnimation(char)
	character = char
	Humanoid = character:WaitForChild("Humanoid")
	Ranimtrack = Humanoid:LoadAnimation(runanim)
end

player.CharacterAdded:Connect(LoadAnimation)

UIs.InputBegan:Connect(function(input, gameProcessed)
	if Humanoid and Humanoid.MoveDirection.Magnitude > 0 then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
			Humanoid.WalkSpeed = Rspeed
			decreasestamina = true
			Toggle()
		end
	end
end)

UIs.InputEnded:Connect(function(input, gameProcessed)
	if Humanoid then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
			Humanoid.WalkSpeed = Nspeed
			decreasestamina = false
			Toggle()
		end
	end	
end)
2 Likes

Hi there,

This is mainly happening due to the way your Toggle() function behaves.

Remember that when LeftShift is pressed, the press itself begins both InputBegin when the key is pressed, and InputEnded when the key is lifted.

Here, if your character is not moving and your Humanoid.MoveDirection.Magnitude is 0, this means your Toggle() function is never called, hence the animation is never played. When you unpress your LeftShift, the Toggle() is then triggered, in turn, toggling the animation on when the key is lifted.

In order to fix this at a baseline, you can make a few minor adjustments to how you change your BoolValue and your Toggle() function.

Give the below code a shot and see if it points you into the right direction:

local UIs = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local localPlayer = Players.LocalPlayer
local Animsfolder = game:GetService("ReplicatedStorage").Anims
local runanim = Animsfolder.RunAnim

local character
local Humanoid
local Ranimtrack

local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local Properties = { FieldOfView = 80 }
local T = game:GetService("TweenService"):Create(Camera, Info, Properties)
local rProperties = { FieldOfView = 70 }
local rInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local rT = game:GetService("TweenService"):Create(Camera, rInfo, rProperties)
--sprintsetting
local BoolValue = false
local Rspeed = 32
local Nspeed = 16
--
local decreasestamina = false
local stamina = 100
--
local startergui = game:GetService("StarterGui")
local staminagui = startergui:WaitForChild("Stamina")
local whitebar = staminagui:WaitForChild("WhiteBar")
local stamcount = whitebar.StaminaCount
local staminanumbervalue = script.Stamina.Value

--

local function Toggle()
	if BoolValue then
		T:Play()
		Ranimtrack:Play()
		repeat wait() until not BoolValue
	else
		rT:Play()
		if Ranimtrack.IsPlaying == true then
			Ranimtrack:Stop()
		end
		repeat wait() until BoolValue
	end
end

local function LoadAnimation(char)
	character = char
	Humanoid = character:WaitForChild("Humanoid")
	--Ranimtrack = Humanoid:LoadAnimation(runanim)
end

player.CharacterAdded:Connect(LoadAnimation)

UIs.InputBegan:Connect(function(input, gameProcessed)
	if Humanoid and Humanoid.MoveDirection.Magnitude > 0 then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
			Humanoid.WalkSpeed = Rspeed
			decreasestamina, BoolValue = true, true
			Toggle()
		end
	end
end)

UIs.InputEnded:Connect(function(input, gameProcessed)
	if Humanoid then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
			Humanoid.WalkSpeed = Nspeed
			decreasestamina, BoolValue = false, false
			Toggle()
		end
	end	
end)

Good luck.

2 Likes

Try removing the “repeat wait() until” loops in the Toggle function

1 Like

Correct me if im wrong but, what im understanding about your solution is when the player unpress the leftshift, the toggle() is triggered. I appreciate you for fixing this,… but the main problem isn’t this but about the run system still running after the player has released ( this is after the player has pressed the leftshift and w key ). So… still thank you for your help but this isn’t the main problem of what i meant

try modifying the Animate script in the player’s character (follow this tutorial) and use a remote event to modify it on the server when pressing left shift in the client

No worries, thank you for specifying. May I ask you to please elaborate slightly on the problem being “the run system still running” bit? After running a short test on this simulating your code conditions, I’m unable to replicate what you’re mentioning. Are you referring to your animation not stopping, or that the Toggle remaining in the incorrect state despite unpressing LeftShift? Sorry if I caused any confusion.

Additionally, is there any specific reason why you’re indefinitely yielding your Toggle() function with repeat wait() until BoolValue?

All humanoid-oriented animations automatically replicate to the server when played on the client without having to incorporate remote communication.

i know that, what i meant is to change the animation in the Animate script, which can only be accessed in the server