Still running after ‘W’ key is released

note: i made the same post 2 weeks ago but i decided to repost it so more people can see it.
So i made a sprint system, it activates when the w and shift key is pressed. But for some reason, in my case, when i pressed shift (only) and let go of the w key and this is after the sprint had started the animations still plays. If anyone could help i would appreciate it!
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 LoadAnimation(char)
	character = char
	Humanoid = character:WaitForChild("Humanoid")
	Ranimtrack = Humanoid:LoadAnimation(runanim)
end

player.CharacterAdded:Connect(LoadAnimation)

--[[
local function CheckWKeyDown()
	while true do
		Wpressed = UIs:IsKeyDown(Enum.KeyCode.W)
		Shiftpressed = UIs:IsKeyDown(Enum.KeyCode.LeftShift)
		print(Wpressed)
		print(Shiftpressed)
		wait()
	end
end
--]]

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

UIs.InputBegan:Connect(function(input, gameProcessed)
	if Humanoid.MoveDirection.Magnitude > 0 then
	local conditions = (input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false)
		while conditions do
			Humanoid.WalkSpeed = Rspeed
			decreasestamina, BoolValue = true, true
			Toggle()
			repeat wait() until not BoolValue or conditions
			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()
			repeat wait() until BoolValue
		end
	end	
end)
2 Likes

if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then

You only checking if LeftShift InputEnded, you should add a check for “w” button,

here is updated code

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

i’ve already tried that before and it doesn’t seem to work
robloxapp-20231219-1017257.wmv

1 Like

What exactly wrong? I dont see anything in your video

1 Like

You should use CAS for this case, as it would fix your issue and probably make it more efficient.

the entire sprint system just doesn’t work for some reason

i’ll try to learn that, thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.