Sprint script keybinds acting weird (repost)

So i want to do a sprinting with stamina,

but the thing is i wanna detect if the player is pressing W/A/S/D with shift so the stamina doesn’t waste without any reason, but if i press shift 1st and then W/A/S/D i cant sprint

I thought it has something to do with the InputBegan function so i tried to experiment…
nothing did work

If you can give me tips about stamina i would be thankfull!

local ViewModel = game.ReplicatedStorage.Models:WaitForChild("Viewmodel")
local SprintAnimation = game.ReplicatedStorage.Animations:WaitForChild("Sprinthold")
local Module = require(game.ReplicatedStorage.Scripts.MainShotgun)
local UIS = game:GetService("UserInputService")
local Stamina = 200
local StaminaActive = false
local StaminaUsing = false
local Exhausted = false
local W = false
local A = false
local S = false
local D = false
local Moving = false -- should detect if the player moves using wasd
local Sprinting = false 
local using = false 



function StartStamina() --is starting the stamina wasting
	while Stamina > 0 and StaminaActive == true do
		Stamina = Stamina - 1
		if Stamina == 0 then
			Exhausted = true
			SprintStop()
			return
		end
		wait(0.01)

		print(Stamina)


	end
end

local function StaminaReset() --is stopping the current stamina wasting

	if StaminaActive == false then
		StaminaUsing = true
		StaminaActive = true
		StartStamina()
	end
end

character = Player.CharacterAdded:Connect(function(character)
	StaminaActive = false




	Stamina = 200-- Max Stamina in Seconds
	if StaminaUsing == false then 
		while Stamina <= 199 do
			Stamina = Stamina + 1
			print(Stamina)

			
			
			wait(0.01)
		end
	end

	UIS.InputBegan:Connect(function(inputM)
		if inputM.KeyCode == Enum.KeyCode.W then
			W = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		if inputM.KeyCode == Enum.KeyCode.A then
			A = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		if inputM.KeyCode == Enum.KeyCode.S then
			S = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		if inputM.KeyCode == Enum.KeyCode.D then
			D = true
			Moving = true
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()

					end



				end		

			end
			return
		end
		UIS.InputEnded:Connect(function(inputW)
			if inputW.KeyCode == Enum.KeyCode.W then
				W = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
		end)
		UIS.InputEnded:Connect(function(inputA)
			if inputA.KeyCode == Enum.KeyCode.A then
				A = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
		end)
		UIS.InputEnded:Connect(function(inputS)
			if inputS.KeyCode == Enum.KeyCode.W then
				S = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
		end)
		UIS.InputEnded:Connect(function(inputD)
			if inputD.KeyCode == Enum.KeyCode.W then
				D = false
				if W == false and A == false and S == false and D == false then
					Moving = false
					Sprinting = false 
					Module.SprintStop()
					character.Humanoid.WalkSpeed = 16
					SprintStop()

				end				

			end
			if inputD.KeyCode == Enum.KeyCode.LeftShift then
				Sprinting = false 
				Module.SprintStop()
				character.Humanoid.WalkSpeed = 16
				SprintStop()

			end
		end)



	end)
	UIS.InputBegan:Connect(function(input) 


		if input.KeyCode == Enum.KeyCode.LeftShift then	
			Sprinting = true	
			if Sprinting == true and Moving == true then
				if Exhausted == false then	
					if using == false then
						using = true
						StaminaActive = false


						Module.SprintStart(SprintAnimation, ViewModel)
						character.Humanoid.WalkSpeed = 32 
						StaminaReset()
						return
					end



				end		

			end
		end	

		return
	end)
	UIS.InputEnded:Connect(function(inputEnd) --should cancel it (it doesn't)

		
	end)






	function SprintStop()
		using = false
		StaminaActive = false
		StaminaUsing = false
		Module.SprintStop()
		character.Humanoid.WalkSpeed = 16
	end

	return

end)

Sorry if this does not help but lets use CAS to knock out all Platforms.

To detect when a player moves we can use

Humanoid.MoveDirection

Note: ^This will change allot.

And instead of a while loop, we’ll use RunService.

In the the code below the first thing that will happen is creating the UI.
Then it will set up the CAS that will bind the Keys to the function “activateBind”.

The code should be self explained. There might be some issues, but I bet you can take the rest from here. :grin:

Click for - Code
--local uis = game:GetService("UserInputService") -- Not Used
local cas = game:GetService("ContextActionService")
local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human:Humanoid = char:WaitForChild("Humanoid",20)

local UIFrame = nil -- ScreenGUI
local barFrame = nil -- Frame

local running = false
local HoldingRun = false
local exhausted = false
local playerMoving = false

local MaxStamina = 100
local stamina = 100
local deplete = 10
local refillTill = 30
local waitToRefill = 2

local canRun = true
local SpeedDiff = 10
local defaultWalk = 16

local sprintCon = nil -- RunService

local function SetUpUI() -- Can remove to put your own
	UIFrame = Instance.new("ScreenGui")
	UIFrame.Parent = player.PlayerGui
	UIFrame.Enabled = false
	UIFrame.Name = "SprintBar"
	local frame = Instance.new("Frame")
	frame.Parent = UIFrame
	frame.Size = UDim2.fromScale(.3,.12)
	frame.Position = UDim2.fromScale(.34,.85)
	frame.BackgroundTransparency = .5
	frame.BackgroundColor3 = Color3.fromRGB(157, 157, 157)
	barFrame = Instance.new("Frame")
	barFrame.Parent = frame
	barFrame.Name = "Bar"
	barFrame.Size = UDim2.fromScale(.97,.82)
	barFrame.Position = UDim2.fromScale(.01,.09)
	barFrame.BackgroundColor3 = Color3.fromRGB(0, 255, 255)
	barFrame.BackgroundTransparency = .5
end

local function sprint(active) -- This will get called multiple times
	if not canRun and active then print("can't Run!") end -- returns with a print
	if not playerMoving and active then return print("player is Idle") end
	if exhausted then return print("pooped out") end 
	if active then
		human.WalkSpeed = defaultWalk + SpeedDiff
	else
		human.WalkSpeed = defaultWalk
	end
	running = active
	local tm = time() -- could have this with other values (I just put it here)
	if active and sprintCon == nil then
		UIFrame.Enabled = true
		sprintCon = runService.Heartbeat:Connect(function(dt:number) -- RunService
			barFrame.Size = UDim2.fromScale(stamina/100,.8) 
			if running then
				if stamina > 0 then
					stamina -= deplete * dt
				else
					tm = time()
					sprint(false)
					exhausted = true
				end
			elseif stamina < MaxStamina then
				if exhausted and time() - tm >= waitToRefill then -- wait in the loop
					stamina += deplete * dt
				elseif not exhausted then
					stamina += deplete * dt
				end
				if stamina > refillTill then
					exhausted = false
					if HoldingRun then
						if running then return print("already running") end
						sprint(true)
					end
				end
			elseif stamina >= MaxStamina then
				sprintCon:Disconnect() -- Removes RunService
				sprintCon = nil
				UIFrame.Enabled = false
			end
		end)
	end
end

local function activateBind(actionName:string,inputState:Enum.UserInputState,inputObject:InputObject)
	if actionName == "Run" then
		if inputState == Enum.UserInputState.Begin then
			HoldingRun = true
			if running then return print("already running") end
			sprint(true)
		elseif inputState == Enum.UserInputState.End then
			HoldingRun = false
			sprint(false)
		end
	end
end

human:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if human.MoveDirection.Magnitude > 0 then
		playerMoving = true
		if running then return print("Already running") end -- will fire allot do to moveDirection changing ever direction
		if HoldingRun then
			sprint(true)
		end
	else
		playerMoving = false
		if HoldingRun then
			sprint(false)
		end	
	end
end)

task.wait(2)
SetUpUI()

cas:BindAction("Run",activateBind,true,Enum.KeyCode.LeftShift,Enum.KeyCode.ButtonL2)

Above all, have a Blessed Day!

1 Like

TYSMM

It works!
Thank you for the whole script.
It helped me so much!

1 Like

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