How to get the children who has a value ticked from a folder?

I’ll try to explane it best to my abilties.

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)]

Thanks for the help ^^

Something like this?

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)]

Then you put this inside of the click event.

3 Likes

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.

2 Likes

is all the boolvalues under game.npcFolder?

Not directly. Boolvalues are inside NPCs. NPCs are inside the npcFolder

Thank you for explaning to me. I’ll try it today to see if it works ^^

Try using a variable.

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.

This one worked for me even thought it doesnt choose it as random. Thank you :slight_smile:

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