Table.clear() not working properly

Hey! I’m trying to do an order kiosk, and I’m currently working on a cancel order button. When a player clicks it, it seems to work fine. When they make a new order and proceed to the pay page (order summary), it first seems fine too, because there aren’t the old items. However, the new item is there more times (x2, x3…) even if it’s the first time ordering it.

I realized that it’s based off the amount of orders the player made, aka if its his fourth order, it would say x4 Roast Beef for example.

I’m not really sure why this happens, because I set few table.clear() functions, in order to clear out tables for saving the order items data.

Below is a video showcasing the problem. (it should say x1 Steak & Cheese, because I ordered it for the first time [and regardless, its a new order, so it should say x1 either way]).

LocalScript (not the whole code, just the parts you need)

local function resetOrder()
	totalPrice = 0
	amountOfOrderItems = 0
	cooldown = true
	isUsingKiosk = false
	playerUsingKiosk = nil
	
	StartMenu.Visible = true
	SummaryMenu.Visible = false
	updateMenusVisibility(false, false, false)
	MainOrderMenu.WhiteSide.ButtonsHolder.SandwichesButton.SelectedLine.Visible = true
	MainButtons.Visible = false
	MainOrderMenu.Visible = false
	MainOrderMenu.WhiteSide.TotalPrice.Text = "$0.00"
	SummaryMenu.Order.Text = ""

	StartMenu:WaitForChild("AdSubway"):TweenPosition(UDim2.new(0.05, 0, 0.15, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
	StartMenu:WaitForChild("OrderButton"):TweenPosition(UDim2.new(0.05, 0, 0.7, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
	task.wait(0.3)

	OccupiedKioskEvent:FireServer("Unoccupied", "SubwayKiosk1")
	cooldown = false
end

local function updateOrderSummary(orderItems, textLabel)
	local itemCount = {}
	local summaryList = {}

	for _, item in ipairs(orderItems) do
		if not itemCount[item] then
			itemCount[item] = 1
			table.insert(summaryList, item)
		else
			itemCount[item] = itemCount[item] + 1
		end
	end

	local formattedList = {}
	for _, item in ipairs(summaryList) do
		local count = itemCount[item]
		table.insert(formattedList, "x"..count.." "..item)
	end

	local summaryText = table.concat(formattedList, "\n")

	textLabel.Text = summaryText
	table.clear(orderItems)
	table.clear(itemCount)
	table.clear(summaryList)
	table.clear(formattedList)
end

MainButtons.PayButton.MouseButton1Click:Connect(function()
	if SummaryMenu.Visible then
	else
		if totalPrice ~= 0 then
			updateMenusVisibility(false, false, false)
			MainOrderMenu.Visible = false
			updateOrderSummary(orderItems, SummaryMenu.Order)
			SummaryMenu:WaitForChild("TotalPrice").Text = "$"..round(totalPrice)
			SummaryMenu.Visible = true
		else
			ClientToClientNotification:Fire({
				Title = "Subway Order Kiosk",
				Content = "You cannot complete an empty order!",
				Time = 3,
				Color = Color3.fromRGB(255, 0, 0)
			})
		end
	end
end)

MainButtons.CancelOrderButton.MouseButton1Click:Connect(resetOrder)

I assume the orderItems is a table you already defined because of this line

In your resetOrder function you do table.clear(orderItems) in order to get rid of every ordered Item

1 Like

Oh yes indeed, its declared at the very start, and its used in another function which just inserts into it 1 order item when you click on the button for it.

Btw, I also tried getting help on GPT (which did help me with some bugs before), and it has no clue at all.

Try printing out the table before it is cleared, after it is cleared, and after the player tries to order again after that and see what you get.

(The code is a little bit different rn, edited, but it still does the same)
Tried that - on the first order, it correctly outprints what was ordered, and after the first order, when the cancel order button is pressed, it correctly outprints that there isn’t anything in the orderItems table.

However, on the second order, it outprints that there are 2 sandwiches instead of 1 that was ordered. If it was the third order, it would outprint 3 of them etc.

How are you saving the items? Is there a specific format you are using?

Also for reference, please point out which table is causing trouble.

Would you like to see the full script? (its not much bigger than the rest I sent before)

I think it would be easier than if I pointed out just specific functions

Yes, of course! I would love to help you out.

1 Like

LocalScript

local TweenService = game:GetService("TweenService")
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CashServiceEvent = ReplicatedStorage.Remotes.SubwayJob.CashService
local guiImagesModule = require(script.SubwayKioskImages)
local ClientToClientNotification = ReplicatedStorage.Remotes.Notifications.ClientToClientNotification
local OccupiedKioskEvent = ReplicatedStorage.Remotes.SubwayJob.OccupiedKiosk

local locPlayer = Players.LocalPlayer
local SurfaceGui = script.Parent
local Kiosk1Box = workspace:WaitForChild("Subway"):WaitForChild("Kiosk1Box")

local StartMenu = SurfaceGui:WaitForChild("StartMenu")
local MainOrderMenu = SurfaceGui:WaitForChild("MainOrderMenu")
local MainButtons = SurfaceGui:WaitForChild("MainButtons")
local SandwichesMenu = SurfaceGui:WaitForChild("SandwichesMenu")
local DrinksMenu = SurfaceGui:WaitForChild("DrinksMenu")
local ExtrasMenu = SurfaceGui:WaitForChild("ExtrasMenu")
local ItemAddedMenu = SurfaceGui:WaitForChild("ItemAddedMenu")
local SummaryMenu = SurfaceGui:WaitForChild("SummaryMenu")

local SandwichesButton = MainOrderMenu.WhiteSide.ButtonsHolder.SandwichesButton
local DrinksButton = MainOrderMenu.WhiteSide.ButtonsHolder.DrinksButton
local ExtrasButton = MainOrderMenu.WhiteSide.ButtonsHolder.ExtrasButton

local cooldown = false
local isUsingKiosk = false
local playerUsingKiosk
local isInKioskZone = false
local totalPrice = 0
local orderItems = {}
local maxOrderItems = 3
local amountOfOrderItems = 0

local function preloadContent()
	ContentProvider:PreloadAsync(guiImagesModule.images, function(contentId, status)
		if status == Enum.AssetFetchStatus.Failure then
			warn("Failed to load", contentId)
		end
	end)
end
preloadContent()

local function round(num)
	return math.floor(num * 100 + 0.5) / 100
end

local function updateMenusVisibility(sandwichesVisible, drinksVisible, extrasVisible)
	SandwichesMenu.Visible = sandwichesVisible
	DrinksMenu.Visible = drinksVisible
	ExtrasMenu.Visible = extrasVisible
	SandwichesButton.SelectedLine.Visible = sandwichesVisible
	DrinksButton.SelectedLine.Visible = drinksVisible
	ExtrasButton.SelectedLine.Visible = extrasVisible
end

local function showItemAddedMessage(item)
	MainButtons.Visible = false
	SandwichesMenu.Visible = false
	DrinksMenu.Visible = false
	ExtrasMenu.Visible = false
	MainOrderMenu.Visible = false
	ItemAddedMenu.Visible = true
	ItemAddedMenu.CircleItem.ItemImage.Image = item.ItemImage.Image
	ItemAddedMenu.CircleItem.Title.Text = "<b>" .. item.ItemName.Text .. "</b> has been added to your order!"
	ItemAddedMenu.CircleItem:TweenPosition(UDim2.new(0.1, 0, 0.25, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0.3)
	totalPrice += tonumber(item.Price.Text:sub(2))
	MainOrderMenu.WhiteSide.TotalPrice.Text = "$" .. round(totalPrice)
	amountOfOrderItems += 1
	table.insert(orderItems, item.ItemName.Text)
	task.wait(1)
	ItemAddedMenu.CircleItem.Position = UDim2.new(0.1, 0, -0.55, 0)
	ItemAddedMenu.Visible = false
	MainButtons.Visible = true
	MainOrderMenu.Visible = true
end

local function handleMenuButtonClick(button, sandwichesVisible, drinksVisible, extrasVisible)
	button.MouseButton1Click:Connect(function()
		updateMenusVisibility(sandwichesVisible, drinksVisible, extrasVisible)
	end)
end

local function handleItemAdded(item, sandwichesVisible, drinksVisible, extrasVisible)
	item.ItemImage.MouseButton1Click:Connect(function()
		if amountOfOrderItems < maxOrderItems then
			showItemAddedMessage(item)
			updateMenusVisibility(sandwichesVisible, drinksVisible, extrasVisible)
		else
			ClientToClientNotification:Fire({
				Title = "Subway Order Kiosk",
				Content = "You cannot order more than 3 items!",
				Time = 3,
				Color = Color3.fromRGB(255, 0, 0)
			})
		end
	end)
end

local function populateMenuItems(menu, sandwichesVisible, drinksVisible, extrasVisible)
	for _, item in pairs(menu.ButtonsHolder:GetChildren()) do
		if item:IsA("Frame") then
			handleItemAdded(item, sandwichesVisible, drinksVisible, extrasVisible)
		end
	end
end

local function printOrderItems()
	print("Current orderItems:")
	for i, item in ipairs(orderItems) do
		print(i, item)
	end
end

local function resetOrder()
	totalPrice = 0
	amountOfOrderItems = 0
	cooldown = true
	isUsingKiosk = false
	playerUsingKiosk = nil
	
	orderItems = {}
	print("Order reset. orderItems should be empty now:")
	printOrderItems()

	StartMenu.Visible = true
	SummaryMenu.Visible = false
	updateMenusVisibility(false, false, false)
	MainOrderMenu.WhiteSide.ButtonsHolder.SandwichesButton.SelectedLine.Visible = true
	MainButtons.Visible = false
	MainOrderMenu.Visible = false
	MainOrderMenu.WhiteSide.TotalPrice.Text = "$0.00"
	SummaryMenu.Order.Text = ""

	StartMenu:WaitForChild("AdSubway"):TweenPosition(UDim2.new(0.05, 0, 0.15, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
	StartMenu:WaitForChild("OrderButton"):TweenPosition(UDim2.new(0.05, 0, 0.7, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
	task.wait(0.3)

	OccupiedKioskEvent:FireServer("Unoccupied", "SubwayKiosk1")
	cooldown = false
end

local function initiateOrder()
	OccupiedKioskEvent:FireServer("Occupied", "SubwayKiosk1")
	cooldown = true
	isUsingKiosk = true
	playerUsingKiosk = locPlayer

	StartMenu:WaitForChild("AdSubway"):TweenPosition(UDim2.new(-1, 0, 0.15, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
	StartMenu:WaitForChild("OrderButton"):TweenPosition(UDim2.new(1, 0, 0.7, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
	task.wait(0.3)

	StartMenu.Visible = false
	MainOrderMenu.Visible = true
	MainButtons.Visible = true
	SandwichesMenu.Visible = true
	cooldown = false
end

StartMenu:WaitForChild("OrderButton").MouseButton1Click:Connect(function()
	if isInKioskZone and not cooldown then
		initiateOrder()
		handleMenuButtonClick(SandwichesButton, true, false, false)
		handleMenuButtonClick(DrinksButton, false, true, false)
		handleMenuButtonClick(ExtrasButton, false, false, true)
		populateMenuItems(SandwichesMenu, true, false, false)
		populateMenuItems(DrinksMenu, false, true, false)
		populateMenuItems(ExtrasMenu, false, false, true)
	end
end)

local function updateOrderSummary(orderItems, textLabel)
	local itemCount = {}
	local summaryList = {}

	for _, item in ipairs(orderItems) do
		if not itemCount[item] then
			itemCount[item] = 1
			table.insert(summaryList, item)
		else
			itemCount[item] = itemCount[item] + 1
		end
	end

	local formattedList = {}
	for _, item in ipairs(summaryList) do
		local count = itemCount[item]
		table.insert(formattedList, "x"..count.." "..item)
	end

	local summaryText = table.concat(formattedList, "\n")
	textLabel.Text = summaryText
	
	orderItems = {}
	printOrderItems()
end

MainButtons.PayButton.MouseButton1Click:Connect(function()
	if SummaryMenu.Visible then
		CashServiceEvent:FireServer(playerUsingKiosk, totalPrice)
	else
		if totalPrice ~= 0 then
			updateMenusVisibility(false, false, false)
			MainOrderMenu.Visible = false
			updateOrderSummary(orderItems, SummaryMenu.Order)
			SummaryMenu:WaitForChild("TotalPrice").Text = "$"..round(totalPrice)
			SummaryMenu.Visible = true
		else
			ClientToClientNotification:Fire({
				Title = "Subway Order Kiosk",
				Content = "You cannot complete an empty order!",
				Time = 3,
				Color = Color3.fromRGB(255, 0, 0)
			})
		end
	end
end)

Kiosk1Box.Touched:Connect(function() isInKioskZone = true end)

Kiosk1Box.TouchEnded:Connect(function(hit)
	if isUsingKiosk and hit.Parent == locPlayer.Character then
		resetOrder()
		isInKioskZone = false
	end
end)

MainButtons.CancelOrderButton.MouseButton1Click:Connect(resetOrder)

CashServiceEvent.OnClientEvent:Connect(function()
	resetOrder()
end)

Correct me if I am wrong: Is this the text that appears on the purchase page (at the end)?

1 Like

Yes indeed. characters bypass

Try running the function updateOrderSummary() AFTER the resetOrder() function.

Also, add a security check in the updateOrderSummary() function, that checks if the orderItems table is empty, so it could return an empty table or an empty string.

I don’t think that would help because the resetOrder function basically brings all of it to the start so then the summary menu wouldn’t even be there.

You also appear to be using the orderItems argument in your functions, which could probably interfere with the actual orderItems table.

Not sure if this roblox allows this,but I always rename my arguments so they aren’t the same as variables.

Also, as for your reply, I don’t think the summary menu needs to be open for my solution to work., it’s only function is to go through the orderItems table and give the text at the end its items from that table, so I figured we could try/.

1 Like

I can for sure try it out, though, where exactly do you mean it?

At the end, you call the resetOrder() function. So after you call that function, call the updateOrderSummary() function

Also I would recommend changing the arguments names in your functions so they aren’t the same as the variables

1 Like

Well the arguments are intended to be the variables, not new arguments, so that’s why its the same :smiley:

1 Like

Just tried it out and unfortunately still does the same. Or maybe you didn’t mean it like this, idk:
obrazek

Yeah, I did mean it like that, weird that it doesn’t work.

Try printing out the orderItems table in the updateOrderSummary() function

I already did that before, and the result was something like this:

First Order: (clicked once on Steak & Cheese)

  • x1 Steak & Cheese → No items in orderItems (after clicking cancel order)

Second Order: (clickd once on Roast Beef)

  • x2 Roast Beef → No items in orderItems (after clicking cancel order)

Therefore the removing (cancelling itself) should work, however, its the adding of new order items on the second and more order. (Once again, if it would be the third order, it would show x3 etc)