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

How can I prevent this script from choosing the same floor twice?

How can I prevent this script from choosing the same floor twice?

function ChooseFloor()
    local Floors = game.ReplicatedStorage.Floors:GetChildren()
    local PickFloor = math.random(1, #Floors)
    local SelectedFloor = Floors[PickFloor]:Clone()
    SelectedFloor.Name = "Floor"
    SelectedFloor.Parent = game.Workspace
end

1 answer

✓ Accepted answer

Try this:

local Floors = game.ReplicatedStorage.Floors:GetChildren()

function ChooseFloor()
    local PickFloor = math.random(1, #Floors)
    local SelectedFloor = Floors[PickFloor]:Clone()
    SelectedFloor.Name = "Floor"
    SelectedFloor.Parent = game.Workspace

    table.remove(Floors, PickFloor)
end

Keep in mind you will run out of floors eventually, so make sure #Floors > 0 evaluates to true before calling the function.

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