Picking a random object from the folder and cloning it?

Hey there devs!

Pretty self-explanatory but what am I trying to do is make an ImageLabel come visible (by tweening) and after 5.5 seconds another random ImageLabel will be chosen and will repeat the exact same thing.

But after tweening 1 or 2 times it throws out an error saying

Players.AridFights1.PlayerGui.MenuGui.MainMenu:101: invalid argument #2 to 'random' (interval is empty) - Client - MainMenu:101

Script
while task.wait(5.5) do
	local GotChildren = Menu_GFX:GetChildren()
	
	local ChosenGFX = GotChildren[math.random(1, #GotChildren)]
	ChosenGFX:Clone().Parent = GFXHolder
	
	local Info = TweenInfo.new(1)
	
	local function Tween(obj, trans)
		local t = TweenService:Create(obj, Info, {ImageTransparency = trans})
		return t
	end
	
	Tween(ChosenGFX, 0)
	
	task.wait(5.5)
	
	local ChosenGFX_2 = GotChildren[math.random(1, #GotChildren)]
	ChosenGFX_2:Clone().Parent = GFXHolder
	
	task.spawn(function()
		Tween(ChosenGFX_2, 0)
		Tween(ChosenGFX, 1)
	end)
	
	task.wait(0.5)
	
	ChosenGFX:Destroy()
	
	task.wait(5.5)
	
	ChosenGFX_2:Destroy()
end

Exporer:

Edit: The tween doesn’t work as well.

local ChosenGFX = GotChildren[math.random(1, #GotChildren)]
ChosenGFX:Clone().Parent = GFXHolder

Here you are selecting a ChosenGFX, creating a clone, and adding the clone to GFXHolder. The issue is, you are not referencing the clone, you are referencing the original ChosenGFX. So when you:

ChosenGFX:Destroy()

You are deleting the original GFX. As you loop, you eventually run out of GFX because you’ve destroyed them all. This makes GotChildren an empty table, hence your error.

To fix, change this:

ChosenGFX:Clone().Parent = GFXHolder

to:

ChosenGFX = ChosenGFX:Clone()
ChosenGFX.Parent = GFXHolder

(and do the same for GFX2)

As for why your tween isn’t working, it’s because you are creating a tween but never playing it.

2 Likes

could you try this

local list = script.Parent:GetChildren()

local random = math.random(1,#list)

for i = 1, random do
	part = list[i]
	if i == random then
		local model = part
		local clone = model:Clone()
		clone.Parent = part.Parent--you can put this to whever you want
	end
end
1 Like

This is really inefficient code. This is equivalent to:

local list = script.Parent:GetChildren()
local rand = math.random(1,#list)

local clone = list[rand]:Clone()
clone.Parent = workspace

There is no need for iteration at all.

1 Like

Well, that fixed the choosing problem, but the tweening doesn’t work. Here’s the updated code:

while task.wait(5.5) do
	local GotChildren = Menu_GFX:GetChildren()
	
	local ChosenGFX = GotChildren[math.random(1, #GotChildren)]
	local Clone = ChosenGFX:Clone()
	Clone.Parent = GFXHolder
	
	local Info = TweenInfo.new(1)
	
	local function Tween(obj, trans)
		local t = TweenService:Create(obj, Info, {ImageTransparency = trans})
		return t
	end
	
	Tween(ChosenGFX, 0):Play()
	
	task.wait(5.5)
	
	local ChosenGFX_2 = GotChildren[math.random(1, #GotChildren)]
	local Clone_2 = ChosenGFX_2:Clone()
	Clone_2.Parent = GFXHolder
	
	task.spawn(function()
		Tween(ChosenGFX_2, 0):Play()
		Tween(ChosenGFX, 1):Play()
	end)
	
	task.wait(0.5)
	
	Clone:Destroy()
	
	task.wait(5.5)
	
	Clone_2:Destroy()
end

It looks like you are attempting to Tween the ChosenGFX and ChosenGFX_2, instead of Clone and Clone_2.

1 Like

thanks I’m quite new to scripting so i don’t really know a lot of this stuff

My brain now… Didn’t got a proper sleep last night so I think I’m going stupid right now.

Thanks for the help! It works greatly!