-- Sprint key pressed or released

replicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(player, state)
	local humanoid = player.Character.Humanoid
	
	if state == "Began" and humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and humanoid.MoveDirection.Magnitude > 0 then
		sprintingPlayers[player.Name] = humanoid.WalkSpeed
		humanoid.WalkSpeed = humanoid.WalkSpeed * sprintModifier
	elseif state == "Ended" and sprintingPlayers[player.Name] then
		humanoid.WalkSpeed = sprintingPlayers[player.Name]
		sprintingPlayers[player.Name] = nil
	end
end)

runService.Heartbeat:Connect(function()
	for index, player in pairs(players:GetChildren()) do
		local stamina = player.Stamina
		local name = player.Name
		
		if not sprintingPlayers[name] then
			if stamina.Value > maxStamina then
				stamina.Value = maxStamina
			elseif stamina.Value < maxStamina then
				stamina.Value = stamina.Value + staminaRegen
			end
		else
			if stamina.Value >= sprintStaminaCost then
				stamina.Value = stamina.Value - sprintStaminaCost
			else
				player.Character.Humanoid.WalkSpeed = sprintingPlayers[name]
				sprintingPlayers[name] = nil
			end
		end
	end
end)
