Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to tell how many objects are in a certain place?

I want a function to connect when the game finds that there is only 1 object in a certain place. Let's say if there's only 1 block named "Brick" in the workspace. How would I tell a script to do something if there's only 1 of them? I have no idea how to even start a script like this.

Thanks for reading

1 answer

✓ Accepted answer

You could use a for loop:

local function howmanyParts(model)
       local nrofparts
       for i, v in pairs(model:GetChildren)) do
              nrofparts = nrofparts + 1
       end
       return nrofparts
end

Or (im not sure if this works though):

local function howmanyParts(model)
       local nrofparts = model:GetChildren()
       return #nrofparts
end

In each example, the model parameter represents the model that contains the number of parts you want to count. So to print the numbers of instances in the workspace you could do print(howmanyParts(game.Workspace)

2 comments

0

(to check if the number of parts is greater than one you can do ...if howmanyParts(model containing the parts) > 1 then...

0

radusavin366, the second script will work, as you want the number of members of a table

This discussion is preserved as it was. New answers, comments, and votes are closed.