How do I find and clone random models from a table?

Hello!

I’m trying to make islands in my game with randomly generated decorations. What would I put in my script which would select a random model from a folder in ServerStorage and place it into the Workspace?

Here’s my script so far:

function createisland()
	
	local island = game.ServerStorage.Island:Clone()

	island.Parent = game.Workspace
	island:PivotTo(CFrame.new(math.random(-3750, 3750), math.random(-3750, 3750), math.random(-3750, 3750)))
	
	local function generatedecorations()
		
		local decoration = {game.ServerStorage.Decorations:GetChildren()}
		local randomdecor = decoration[math.random(1, #decoration)]
		
		randomdecor.Parent = game.Workspace
		randomdecor:PivotTo(island.WorldPivot + Vector3.new(math.random(-125, 125), 0, math.random(-125, 125)))
	
			end
	
	generatedecorations()
	
		end

while true do
	
	createisland()
	wait(5)
	
		end

Also, I apologize if I am not concise here. This is my first ever post. If I did something wrong, please tell me!

GetChildren gives you a table of the children so this line of code is putting a table in a table. Here’s how you can fix this:

local decoration = game.ServerStorage.Decorations:GetChildren()

You could do this,


local decoration = game.ServerStorage.Decorations:GetChildren()
local randomdecor = decoration[math.random(1, #decoration)]

I just removed the table here because GetChildren automatically gives you a table, so you shouldn’t be putting the children in another table

Thanks so much!! This works perfect :smiley:

Diabolical snub lol

RizzRizz

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.