Assigning every NPC a custom seat

I have a value, right?
This value defines a seat that the NPC will pathfind to when it is told to…
However, this isnt very efficient as I have to go through every NPC and assign them their own seat value.
Anyway to make this automatic? I.e, if a seat is taken it will pick another seat?
Thanks :hidere:

I would imagine you could look for Seat Occupant and if it is not nil, then set that as the target for the NPC Pathfinding.

Heres the problem, they all go to their seats at once…

You could also make a dictionary for the seats, with the object as the key and occupied boolean as value, and loop through a table of the npcs to find the first empty in the seats dictionary. When a npc is assigned to a seat, set the value to true, even while it is still moving.

Never used dictionaries, I’ll read up and get back to you if this works!

1 Like

This isnt tested, but you could write something like this.

local npcs = {workspace.npc1, workspace.npc2, workspace.npc3}
local seats = {
    [workspace.seat1] = false,
    [workspace.seat2] = false,
    [workspace.seat3] = false
}

for i,npc in ipairs(npcs) do
    local firstAvailSeat = nil
    for seat,taken in pairs(seats) do
        if not taken then
            seats[i] = true
            firstAvailSeat = seat
            break
        end
    end
    if firstAvailSeat then 
        pathFindFunc(npc, firstAvailSeat)
    else
        print("no seats left")
    end
end
1 Like

Or, of course, you could always write some code of your own, which I recommend since I haven’t tested this code yet.

Always appears that there are no seats left. (Tho there is most deffinetly seats left.)

local npcs = game.Workspace["El Prisoners"]:GetChildren()
local seats = {
	seat1 = game.Workspace.Seats.Seat1 == false,
	seat2 = game.Workspace.Seats.Seat2 == false,
	seat3 = game.Workspace.Seats.Seat3 == false
}

for i,npc in ipairs(npcs) do
	for seat,taken in pairs(seats) do
		if seat[i] == false then
			seat[i] = true
			EpicSeat = seat
		end
	end
	if EpicSeat ~= nil then 
		print(EpicSeat,npc.Name)
	else
		print("nope")
	end
end

Wait, I know why. when the script finds a seat, it continues looping and marks all the seats taken.
insert a break right after firstAvailSeat = seat

ex

for seat,taken in pairs(seats) do
    if not taken then
        seats[i] = true
        firstAvailSeat = seat
        break
    end
end

Also, you deleted the local EpicSeat = nil right after for i,npc in ipairs(npcs) do. That needs to be there to define the variable “epicseat”.

No difference sadly, still prints Nope

for i,npc in ipairs(npcs) do
	local EpicSeat=nil
	for seat,taken in pairs(seats) do
		if seat[i] == false then
			seat[i] = true
			EpicSeat = seat
			break
		end
	end
	if EpicSeat ~= nil then 
		print(EpicSeat,npc.Name)
	else
		print("nope")
	end
end

do

        if seats[seat] == false then
			seats[seat] = true
			EpicSeat = seat
			break
		end

in other words for the loop invoking the dictionary replace i with seat

Thanks alot, this works perfectly!

1 Like

One last question however.
How would I get the whole game.workspace.Seats.Seat2? Intending to put the EpicSeat variable into a ObjectValue. Currently when I try it EpicSeat is a string.

seat2 = game.Workspace.Seats.Seat2 == false

[edit solved]