Code issues verifying Username in Orders Table

Hello! I’m trying to verify the username in the orders table but it’s returning false. Any idea what I might be doing wrong when adding data to my table?

localscript:

local orders = {}

local function saveOrderData()
	-- Get the selected items
	local selectedItems = {}
	for _, button in ipairs(clickedButtons) do
		local itemName = buttonNames[button.Name]
		if itemName then
			table.insert(selectedItems, itemName)
		end
	end

	local totalCost = calculateTotalPrice()

	-- Create the order object
	local order = {
		username = Players.LocalPlayer.Name, -- Include the username
		item1 = selectedItems[1] or "",
		item2 = selectedItems[2] or "",
		totalprice = totalCost
	}

	print("Order data obtained:")
	print("Username:", order.username)
	print("Item1:", order.item1)
	print("Item2:", order.item2)
	print("Total Price:", order.totalprice)

	-- Add the order to the orders table
	table.insert(orders, order)

	return order -- Return the order table
end

-- Function to check if the order exists in the orders table
local function checkOrderExists(username)
	print("Checking if order exists for username:", username)
	for _, order in pairs(orders) do
		if order.username == username then
			print("Order found for username:", username)
			return true -- Order found
		end
	end
	print("Order not found for username:", username)
	return false -- Order not found
end

-- Connect click event for yes request order button
buttonYesRequestOrder.MouseButton1Click:Connect(function()
	-- Get the selected items
	local selectedItems = {}
	for _, button in ipairs(clickedButtons) do
		-- Use the buttonNames table to get the item name
		local itemName = buttonNames[button.Name]
		if itemName then
			table.insert(selectedItems, itemName)
		end
	end

	-- Get the order data
	local orderData = saveOrderData()
	local totalPrice = calculateTotalPrice()

	if totalPrice > 0 then
		resetOrder()
		ReplicatedStorage.remoteEvents:WaitForChild("close_OrderGUI"):FireServer()
		toggleLoadingOrderSent(true)
		wait(3)
		toggleLoadingOrderSent(false)
		wait(0.2)

		local playerTeamName = Players.LocalPlayer.Team and Players.LocalPlayer.Team.Name
		if playerTeamName == "Customer" then
			local clonedTemplateEvent = game:GetService("ReplicatedStorage").orderTemplate:WaitForChild("clonedTemplateEvent")
			-- Pass order data as parameters to clonedTemplateEvent:FireServer()
			clonedTemplateEvent:FireServer(orderData.username, orderData.item1, orderData.item2, orderData.totalprice)
		else
			print("You are not in the Customer Team.")
		end
		resetButtonColor() -- Reset button color
	else
		warn("There are no items in the order or a template already exists.")
	end
end)

-- Connect the MouseButton1Click event for the deliveryOrderButton
deliveryOrderButton.MouseButton1Click:Connect(function()
	print("Delivery order button pressed")

	local nearbyCustomer = checkNearbyCustomer() 
	local orderExistsForCustomer = checkOrderExists(nearbyCustomer) 

	if orderExistsForCustomer then
		print("Order found for nearby customer:", nearbyCustomer)
	else
		print("No order found for nearby customer:", nearbyCustomer)
	end

	printOrdersTable()
end)

You haven’t included the checkNearbyCustomer() function, but if it returns a Player object, it will be comparing a username with a Player instance in the checkOrderExists() function, returning false.

I’ve attempted this solution, but unfortunately, it’s still not functioning as expected. :frowning: The issue persists with the order table not being updated.

local function checkOrderExists(username)
    if username == "" then
        print("No nearby customer found.")
        return false
    end
    
    print("Checking if order exists for username:", username)
    for _, order in pairs(orders) do
        if order.username == username then
            print("Order found for username:", username)
            return true
        end
    end
    print("Order not found for username:", username)
    return false
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.