Randomly select a decal

Hi! I’m attempting to create a script that selects one of several random Decals -
I used to have a method of doing this, however I cannot for the life of me remember how I did it.
The way I had done it before didn’t require me to name the Decals different number and do a math.random etc etc. It simply let me get one of the Children at random.

I believe it to have been along the lines of:

local faces = game.Workspace.Faces:GetChildren()
local Randomface = 1,#faces

But I’m not certain what so ever.
Is anyone able to help rejog my memory on this one? :L

image

Use Random.new() and NextInteger. E.g.

local r = Random.new()

local n = r:NextInteger(1, 5) -- random number between 1 and 5, including 1 and 5
2 Likes

You can easily do this using Random.new

Building off of @EchoReaper’s example, you can also choose that value from the table like this:

local faces = game.Workspace.Faces:GetChildren()
local rand = Random.new()
local num = rand:NextInteger(1,#faces)
local chosen = faces[num]
2 Likes

Thanks guys!
This isn’t the one I’m familiar with but it works regardless!

You may have used math.random(), but Random.new() is the newer version.

math.random() does use Random.new now, but Random.new is the newest way.

Using this Method, am I able to create a list that’s not Random?

So, where this seems to use:
local rand = Random.new()
local num = rand:NextInteger(1,#HairStyles)

Would I be able to skip the Random part and have it go to a next one?

First we’ll need to create a table of your decals.

 local Decals = Faces_Folder:GetChildren() --Creates a table of all the children of your faces folder

 local Decals_Index = 1 --Change this to whatever index you want from our newly created table

Decals[Decals_Index].Parent = game.Workspace.Decal_Part --Do something with the indexed decal

Hope that helps. :slight_smile:

1 Like

If you want to iterate slowly though the list, you can use the following:

local faces = game.Workspace.Faces:GetChildren()
local cur = 1

local function chooseDecal()
    local obj = faces[cur]
    cur = cur + 1
    if cur>#faces then
        cur = 1
    end
    return obj
end
print(chooseDecal()) --Prints first object of faces
print(chooseDecal()) --Prints second object of faces
print(chooseDecal()) --If only 2 objects were in faces, this would print the first one
2 Likes