local b=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local r=require(script.Parent.StaminaModule)local y=game:GetService("\084\119\101\101\110\083\101\114\118\105\099\101")local X=game.Players.LocalPlayer local l=16 local n=18 local T=24 local N=1 local q=.1 local u=15 local a=false local J=1 local A=2 local function g()while a and r.StaminaValue.Value>0 do local b=X.Character if b then local y=b:WaitForChild("\072\117\109\097\110\111\105\100")if y.MoveDirection.Magnitude>0 then r.RemoveStamina(N)r.StaminaCanRegen=false else r.StaminaCanRegen=true end end wait(q)end end local function B(b,r,X)local l=TweenInfo.new(X)local n=y:Create(b,l,{WalkSpeed=r})n:Play()end b.InputBegan:Connect(function(b)if b.KeyCode==Enum.KeyCode.LeftShift then local b=X.Character if b then local y=b:WaitForChild("\072\117\109\097\110\111\105\100")if r.StaminaValue.Value>=u then B(y,T,1)a=true r.StaminaCanRegen=false g()end end end end)b.InputEnded:Connect(function(b)if b.KeyCode==Enum.KeyCode.LeftShift then local b=X.Character if b then local y=b:WaitForChild("\072\117\109\097\110\111\105\100")B(y,l,1)a=false wait(J)r.StaminaCanRegen=true end end end);(r.StaminaValue:GetPropertyChangedSignal("\086\097\108\117\101")):Connect(function()local b=X.Character if b then local y=b:WaitForChild("\072\117\109\097\110\111\105\100")if r.StaminaValue.Value<u and a then B(y,n,2)end if r.StaminaValue.Value<=0 then B(y,l,1)a=false wait(A)r.StaminaCanRegen=true end end end)


local UserInputService = game:GetService("UserInputService")
local StaminaModule = require(script.Parent.StaminaModule)
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer

local defaultSpeed = 16
local belowStaminaSpeed = 18
local sprintSpeed = 24
local staminaDeductRate = 1
local staminaDeductInterval = 0.1
local minimumStaminaToSprint = 15
local sprinting = false
local regainDelay = 1
local belowStaminaDelay = 2

local function deductStamina()
	while sprinting and StaminaModule.StaminaValue.Value > 0 do
		local character = player.Character
		if character then
			local humanoid = character:WaitForChild("Humanoid")
			-- Only deduct stamina if the player is moving
			if humanoid.MoveDirection.Magnitude > 0 then
				StaminaModule.RemoveStamina(staminaDeductRate)
				StaminaModule.StaminaCanRegen = false
			else
				StaminaModule.StaminaCanRegen = true
			end
		end
		wait(staminaDeductInterval)
	end
end

local function tweenWalkSpeed(humanoid, speed, time)
	local info = TweenInfo.new(time)
	local tween = TweenService:Create(humanoid, info, {WalkSpeed = speed})
	tween:Play()
end

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local character = player.Character
		if character then
			local humanoid = character:WaitForChild("Humanoid")
			if StaminaModule.StaminaValue.Value >= minimumStaminaToSprint then
				tweenWalkSpeed(humanoid, sprintSpeed, 1)
				sprinting = true
				StaminaModule.StaminaCanRegen = false
				deductStamina()
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local character = player.Character
		if character then
			local humanoid = character:WaitForChild("Humanoid")
			tweenWalkSpeed(humanoid, defaultSpeed, 1)
			sprinting = false
			wait(regainDelay)
			StaminaModule.StaminaCanRegen = true
		end
	end
end)

StaminaModule.StaminaValue:GetPropertyChangedSignal("Value"):Connect(function()
	local character = player.Character
	if character then
		local humanoid = character:WaitForChild("Humanoid")
		if StaminaModule.StaminaValue.Value < minimumStaminaToSprint and sprinting then
			tweenWalkSpeed(humanoid, belowStaminaSpeed, 2)
		end
		if StaminaModule.StaminaValue.Value <= 0 then
			tweenWalkSpeed(humanoid, defaultSpeed, 1)
			sprinting = false
			wait(belowStaminaDelay)
			StaminaModule.StaminaCanRegen = true
		end
	end
end)
