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

Is it possible to do something untill something dose not exist?

So im trying to make a stand system where you picks a random stand but I dont want it to pick a stand with a value named "Private" in it. I just dont know a efficant way to keep picking a random one untill it finds one without the tag. This is the script that picks it:

local Stand = game:GetService("ServerStorage").Stands

local items = Stand:GetChildren()

local randomStand = items[math.random(1, #items)]
if randomStand:FindFirstChild("Private") == nil then
	return
else
	local randomStand = items[math.random(1, #items)]
end

Thanks in advanced

2 answers

✓ Accepted answer

You need to filter out private stands with public ones and then select random one from the public ones, using ipairs loop it should not be a problem

local Stand = game:GetService("ServerStorage").Stands

local items = Stand:GetChildren()

local public = {}

for _, Item in ipairs(items)
do
	if not (Item:FindFirstChild("Private"))
	then
		table.insert(public, Item)
	end
end

local randomStand = public[math.random(1, #public])

I hope you get the idea. Read about loops here if you don't understand them.

If you want to repeat something until stand is not picked, you can simply repeat a function if the stand is not private, can look like this

local function FindStand()

	local randomStand = items[math.random(1, #items])

	if (randomStand:FindFirstChild("Private"))
	then
		FindStand()
	else
		print('Found')
	end
end

FindStand()

If the stand is private, it will call FindStand again which will do the same thing but will select other stand.

2 comments

0

What is the varible for the awser of the FindStand() (The selected stand)

0

In the FindStand function on line 9 you can replace print('Found') with return randomStand, that will return the stand that has been randomly picked. Or this is not what you mean?

local Stand = game:GetService("ServerStorage").Stands

local items = Stand:GetChildren()

local randomStand = items[math.random(1, #items)]
if randomStand:FindFirstChild("Public") == nil
    return
else
    local randomStand = items[math.random(1, #items)]

local randomStand = items[math.random(1, #items)]
if randomStand:FindFirstChil("Private") == nil then


--Idk if this is the right answer so ye test it out.

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