Script Causing Game To Freeze Upon Pressing Button Multiple Times

Over the week, I have been essentially working on a “vehicle pool” system. It basically works in a UI that has the ability for the player to check vehicles in and out. This then allows for a limited amount of vehicles to be in the game at all times. I have not completed the script at all, but have gotten everything done UI-wise and have made it deduct and add vehicles depending on what you press. It works by having a module script which then the local script reads and adds the tables onto the UI with a UIListLayout.

here’s the problem
Whenever I consistently press the “check-in” and “check-out” buttons respectively, the game is fine until it begins to freeze to the point where the studio is frozen for over 5 seconds and gets worse and worse. I read up on “memory leaks” but I don’t know if that’s the root cause of this.

I want assistance on how to fix this issue and what I can do to prevent it in the future.

local script inside of UI

--Variables
local Vehicles = require(game.ReplicatedStorage.VehiclePoolModules.ACSOpool)
local ExampleButton = script.Parent.ExampleButton
local ExampleTab = script.Parent.ExampleTab
local ExampleCheckedOutVehicle = script.Parent.ExampleCheckedVehicle
local CheckInButton = script.Parent.CheckInButton
local CheckOutButton = script.Parent.CheckOutButton
local checkInVehicleEvent = game.ReplicatedStorage.VehiclePoolRemotes.CheckInVehicle
local checkOutVehicleEvent = game.ReplicatedStorage.VehiclePoolRemotes.CheckOutVehicle
local ACSOPoolFolder = game.ReplicatedStorage:FindFirstChild("ACSOPool")
local selectedFolder
local selectedData


--Functions

local function clearUI()
	for i, child in pairs(script.Parent.ScrollingFrame:GetChildren()) do
		if child.ClassName == "TextButton" then
			child:Destroy()
		end
	end
end

local function updateInfoBar(data)
	script.Parent.InfoBar.VehicleName.Text = data[1]
	local Vehicle = script.Parent.InfoBar.VehicleName.Text
	local vehicleValue = ACSOPoolFolder:FindFirstChild(selectedFolder):FindFirstChild(Vehicle)
	script.Parent.InfoBar.Quantity.Text = "Currently Available: ".. vehicleValue.Value
	vehicleValue.Changed:Connect(function()
		updateInfoBar(data)
	end)
end

for k, v in pairs(Vehicles) do
	if type(v) == "table" then
		local newTab = ExampleTab:Clone()
		newTab.Text = k
		newTab.Parent = script.Parent.Tabs
		newTab.Name = k
		newTab.MouseButton1Click:Connect(function()
			selectedFolder = k
			clearUI()
			wait(0.1)
			for _, data in pairs(v) do
				local newButton = ExampleButton:Clone()
				newButton.Text = data[1]
				newButton.MouseButton1Click:Connect(function()
					selectedData = data
					updateInfoBar(data)
				end)
				newButton.Parent = script.Parent.ScrollingFrame
			end
		end)
	end
end

local selectedVehicles = {}

local selectedVehicles = {}
CheckInButton.MouseButton1Click:Connect(function()
	local selectedVehicle = script.Parent.InfoBar.VehicleName.Text
	if selectedVehicle then
		if selectedVehicles[selectedVehicle] then
			print("You have already selected that vehicle.")
			return
		end
		selectedVehicles[selectedVehicle] = true
		checkInVehicleEvent:FireServer(selectedFolder, selectedVehicle)
		for i,v in pairs(Vehicles) do
			for a,d in pairs(v) do
				if d[1] == selectedVehicle then
					selectedData = d
					break
				end
			end
		end
		updateInfoBar(selectedData)
		local selectedVehicleButton = script.Parent.ScrollingFrame:FindFirstChild(selectedVehicle, true)
		if selectedVehicleButton then
			print("cool")
		end
	else
		print("No vehicle selected.")
	end
end)
CheckOutButton.MouseButton1Click:Connect(function()
	local selectedVehicle = script.Parent.InfoBar.VehicleName.Text
	if selectedVehicle then
		if not selectedVehicles[selectedVehicle] then
			print("You have not selected that vehicle.")
			return
		end
		selectedVehicles[selectedVehicle] = nil -- remove the vehicle from the selected vehicles table
		checkOutVehicleEvent:FireServer(selectedFolder, selectedVehicle)
		for i,v in pairs(Vehicles) do
			for a,d in pairs(v) do
				if d[1] == selectedVehicle then
					selectedData = d
					break
				end
			end
		end
		updateInfoBar(selectedData)
	else
		print("No vehicle selected.")
	end
end)

server script inside SSS that handles the remoteevents

local checkInVehicleEvent = game.ReplicatedStorage.VehiclePoolRemotes.CheckInVehicle
local checkOutVehicleEvent = game.ReplicatedStorage.VehiclePoolRemotes.CheckOutVehicle
local ACSOpool = require(game.ReplicatedStorage.VehiclePoolModules.ACSOpool)
local MAKEAREMOTE = game.ReplicatedStorage.VehiclePoolRemotes.FireClients

local Folder = Instance.new("Folder")

Folder.Name = "ACSOPool"
Folder.Parent = game.ReplicatedStorage
local ACSOPoolFolder = game.ReplicatedStorage:FindFirstChild("ACSOPool")
for i, v in pairs(ACSOpool) do
	local q = Instance.new("Folder")
	q.Name = i
	q.Parent = Folder

	for a, d in pairs(v) do
		local value = Instance.new("NumberValue")
		value.Name = d[1]
		value.Value = d[2]
		value.Parent = q
	end
end 

checkInVehicleEvent.OnServerEvent:Connect(function(player, selectedFolder, selectedVehicle, callback)
	local vehicleValue = ACSOPoolFolder:FindFirstChild(selectedFolder):FindFirstChild(selectedVehicle)
	if vehicleValue then 
		vehicleValue.Value = vehicleValue.Value - 1 
		if vehicleValue.Value <= 0 then
			vehicleValue.Value = 0
		end
	end
end)

checkOutVehicleEvent.OnServerEvent:Connect(function(player, selectedFolder, selectedVehicle)
	local vehicleFolder = ACSOPoolFolder:FindFirstChild(selectedFolder)
	if not vehicleFolder then
		print("No folder found for the selected vehicle type.")
		return
	end

	local vehicleValue = vehicleFolder:FindFirstChild(selectedVehicle)
	if not vehicleValue then
		print("No vehicle found with the selected name.")
		return
	end

	local newValue = vehicleValue.Value + 1
	vehicleValue.Value = newValue
	if newValue >= 0 then
		print("Successfully checked out vehicle: " .. selectedVehicle)
	else
		vehicleValue.Value = 0
		print("Vehicle pool is empty.")
	end
end)

module script inside of replicated storage

local ACSOpool = {
	Marked = {
		{"2022 Ford Explorer", 5},
		{"2021 Chevrolet Tahoe", 5},
		{"2020 Chevrolet Tahoe", 10},
		{"2017 Chevrolet Tahoe", 10},
		{"2016 Chevrolet Impala", 8},
		{"2016 Chevrolet Caprice Highway", 2}
	},
	Unmarked = {
		{"2022 Ford Explorer Unmarked", 3},
		{"2020 Chevrolet Tahoe Unmarked", 2},
		{"2017 Chevrolet Tahoe Unmarked", 5},
		{"2020 Hyundai Sonata Undercover", 2},
		{"2016 Hyundai Sonata Undercover", 1},
		{"2014 Chevrolet Impala Undercover", 2}
	},
	Special = {
		{"2022 Ford Transit", 2},
		{"2010 Lenco Bearcat SERT", 1},
		{"2004 GMC Yukon XL SERT", 1}
	}
}
return ACSOpool