Something wrong with tables

local seatcheck = {}

local availableseats = {}
local takenseats = {}
local done = true

function seatcheck.seatchecker()
	if done then
		done = false
		local seatfolder = game.Workspace:WaitForChild("seats") -- Waits for seats folder

		for i, seat in ipairs(seatfolder:GetChildren()) do
			if seat:IsA("Seat") then -- Make sure it's actually a seat
				if seat.Occupant == nil then
					if table.find(availableseats, seat.Name) then
						
					else
						table.insert(availableseats, seat)
					end
				else
					if table.find(takenseats, seat.Name) then
						
					else
						table.insert(takenseats, seat)
					end
				end
			end
		end

		done = true -- Set done to true after everything is complete
	end
end

function seatcheck.getmeAseat()
	if #availableseats == 0 then
		warn("No available seats.")
		return nil -- Return nil if no seats are available
	end

	local takingAseat = availableseats[math.random(1, #availableseats)]
	
	availableseats[takingAseat] = nil 
	table.insert(takenseats, takingAseat)

	return takingAseat
end

return seatcheck

this script is off a simple restaurant game, but something adds duplicates into the availabeSeats table. iā€™m going to lose my mind pls help

In these lines you are looking for the seat.Name in the availableseats array, but what you stored in the array is the Instance itself (the object) not the name, so it will store always the seat Instance, cause it will never find the name cause you never stored it:

				if seat.Occupant == nil then
					if table.find(availableseats, seat.Name) then

					else
						table.insert(availableseats, seat)
					end
				else
					if table.find(takenseats, seat.Name) then

					else
						table.insert(takenseats, seat)
					end
				end

You could choose to store the Instances or the names, whichever you prefer

1 Like