I have a folder. Inside the folder, there are npcs (who are going to be waiters). Every NPC has a Bool Value, which turns true whenever a waiter is busy. I’m writing a script where when a player clicks on a button, a random waiter who isnt busy will show up.
However, how do I refer to the npcs who are checked “false” in the folder?
Here is what I thought might work for selecting a random waiter:
local NPC = game.npcFolder:GetChildren()
--at this point the game needs to get children with the boolvalue false, which will be named avaible npcs
clickDetector.MouseClick:Connect(function()
local playerNPC = avaibleNPC[math.random(1, #avaibleNPC)]
local NPCsToFilter = game.npcFolder:GetChildren()
local availableNPCs = {}
for _,npc in NPCsToFilter do
if not npc.boolvalue.Value then
table.insert(availableNPCs, npc)
end
end
local chosenNPC = availableNPCs[math.random(1, #availableNPCs)]
Assuming you are using Attributes, You can do this:
local local playerNPC = avaibleNPC[math.random(1, #avaibleNPC)]
if not PlayerNPC:GetAttribute("Busy") then
-- code
But, There are flaws in this code, this code can choose a waiter that is already Busy, or was choosen by someone else playing the game, which can lead to some issues, for this, we can create 3 tables for each state they are in:
local Busy = {} -- For the Waiters who are Busy
local Available = {} -- Waiters Ready to Work
local Working = {} -- This Table is so we can store our Chosen waiter without them being chosen again by another person
So when a Available Waiter is chosen, you can do this:
table.remove(Available, Available[NPC}) -- removes NPC from list
table.insert(Working, NPC) -- adds NPC to other list
You don’t have to do the Tables however, Its just a way to be organized.
local yournpcfolder = workspace.Folder:GetChildren()
local ChosenNPCnotBusy = nil
for _, npc in pairs(yournpcfolder) do
if npc.Busy.Value == false then
ChosenNPCnotBusy = npc
break
else
continue
end
end
-- then the npc thats not busy is chosen at a variable.