Basically I have a stamina system for sprinting which is below, however currently if you spam shift really fast the stamina regen function in the InputEnd area will repeat for each time you release shift causing you to regen stupidly fast.

I've tried to the best of my limited knowledge to figure it out but couldn't so here I am.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Input = game:GetService("UserInputService")
local Sprinting = Player.PlayerScripts:WaitForChild("Core").Sprinting

local Stamina = 100


Mouse.KeyDown:Connect(function(key)
	if key:byte() == 32 then
		Stamina = Stamina - 7
	end
end)


Input.InputBegan:Connect(function(input)
	if(input.KeyCode==Enum.KeyCode.LeftShift) then
		Sprinting.Value = true
		local Humanoid = game.Players.LocalPlayer.Character.Humanoid
		if Humanoid then
		Humanoid.WalkSpeed = 25	
		end	
		while Sprinting.Value == true and Stamina >= 6 do
			if Stamina >=6 then
			Stamina = Stamina - 1
			wait(.05)
			end
		end
		Humanoid.WalkSpeed = 16
		Sprinting.Value = false
	end
end)

Input.InputEnded:Connect(function(input)
	if(input.KeyCode==Enum.KeyCode.LeftShift) then
		Sprinting.Value = false
		local Humanoid = game.Players.LocalPlayer.Character.Humanoid
		if Humanoid then
			Humanoid.WalkSpeed = 16
		end
		while Sprinting.Value == false and Stamina <=99 do
			if Stamina <=99 then
			Stamina = Stamina + 1
			wait(.7)
			end
		end
	end
end)

2 comments

0

You should NOT be using KeyDown. It is deprecated.