Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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.

0
What is the varible for the awser of the FindStand() (The selected stand) Ems_Squad 58 — 3y
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? imKirda 4491 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
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.

Answer this question