Clones [amount] random buttons [Completed]

Clone 3 random buttons from a frame.

I tried to clone 3 random buttons but it still clone 1 button, no errors.

image

local Folder = script.Parent.ItemLists:GetChildren()

local Child = Folder[math.random(3, #Folder)]:Clone()

if Child:IsA("TextButton") then
	Child.Parent = script.Parent.Items
end

Hope you understand what I meant.

1 Like

Use a for i,v in pairs / ipairs loop

Wrong kind of loop, but close.
You would actually want a for i = 1, math.random(1, 3) do loop.

There you go @Rexiler1

local rand = Random.new()
local Folder = script.Parent.ItemLists:GetChildren()

for count = 1, 3 do
     local Child = Folder[rand:NextInteger(3, #Folder)]:Clone()
     if Child:IsA("TextButton") then
	     Child.Parent = script.Parent.Items
     end
end

Try this @Rexiler1:

local Folder = script.Parent.ItemLists:GetChildren()

for i = 1, 3 do
	local Child = Folder[math.random(3, #Folder)]:Clone()
	if Child:IsA("TextButton") then
		Child.Parent = script.Parent.Items
	end
end

Thanks you @SnowyStxrm and @neeols
Your both scripts actually works, idk which one to mark solution lol.

Use mine, using Random.new() and rand:NextInteger is unneeded. math.random() will suffice. (plus he literally just copied my code and edited it to use Random.new() and rand:NextInteger, editing his post after mine to make it look like I copied him…)

1 Like

image

seems like it cloned the same button instead of different one :moyai:

change the three in math.random to a 1 and see if that fixes it

image

still happening though :moyai:


for count = 1,5 do


local Child = Folder[math.random(3, #Folder)]:Clone()
end

Bro what? I posted mine 20 mins before yours! I came back and edited it later. Pretty sure NextInteger is needed here since its a loop.

local Child = Folder[math.random(3, #Folder)]:Clone()
local old = Child
for count = 1,5 do

if old == Child then
Child = Folder[math.random(3, #Folder)]:Clone()
Child:Destroy()
else
Child = Folder[math.random(3, #Folder)]:Clone()
old = child
end
end

Your code was not that. Furthermore rand:NextInteger is not needed because math.random() is called every time.

Do you want them to never duplicate?

image

Nvm, I changed something inside the script, it works now

1 Like

No worries. Don’t forget to mark my post as a solution!

You are like in need of a solution

Using Random.new() and rand:NextInteger is actually needed, the reason why it’s working because I use Random.new() and rand:NextInteger
Instead of cloning the same button, it clones a different buttons

1 Like

local random = folder[math.random(1, #folder)]:Clone()

for count = 1,5 do

local folder = game.ReplicatedStorage.Buttons:GetChildren()

local random = folder[math.random(1, #folder)]:Clone()

random.Parent = game.Players.mymomtookmyps5away.PlayerGui.ScreenGui.Frame

end