--------------------


local function ChangeSpeed(spd)
	local char = plr.Character
	local humanoid
	if char then
		humanoid = char:FindFirstChildWhichIsA("Humanoid")
	end
	if humanoid.WalkSpeed ~= spd then -- Preventing remote spam.
		event:FireServer(1055299,spd)
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = true
	end
end)
game:GetService("UserInputService").InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
		running = false
	end
end)

while wait() do
	if enabled.Value == true then
		if humanoid then
			if humanoid.MoveDirection == Vector3.new(0,0,0) then moving = false else moving = true end
			if tired then 
				ChangeSpeed(exhaust.Value) -- Rest
				Frame.Rest1.Visible = true
				Frame.Rest2.Visible = true
				if not moving then
					stamina = stamina + 1
				end
				if stamina > 0 then
					tired = false
					Frame.Rest1.Visible = false
					Frame.Rest2.Visible = false
					Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
				end
			elseif running == true then
				if moving then
					ChangeSpeed(run.Value) -- Sprint
					stamina = stamina - 1
					Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
					if stamina == 0 then tired = true stamina = -10 end
				elseif stamina < 250 then
					stamina = stamina + 1
					Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
				end
			else ChangeSpeed(walk.Value) -- Walk
				if not moving and stamina < 250 then
					stamina = stamina + 1
					Frame.GreenBar:TweenSize(UDim2.new(3.5*stamina/875,0, 0,25), "Out", "Quad", 0.1, true)
				end
			end
		end
	end
end
