Random clone model

I’d like to know, how to make the game choose for example a random model in the Replicated Storage and the clone.

3 Likes

You can use GetChilren() to get all the children of replicated storage in an array, then you can use math.random(lowest, highest) to pick a random part in that array.

local Children = game.ReplicatedStorage:GetChildren()
local AmountOfChildren = #Children  
local RandomPositionInArray = math.random(1, AmountOfChildren)
local RandomChild = Children[RandomPositionInArray] 

If you don’t fully understand this, read up on tables (arrays are types of tables) and math.random on the dev hub, they’re very simple.

Can you explain what you mean by that?

1 Like

I suggest putting your models in the ServerStorage and use ServerStorage:GetChildren()[math.random(#models)]:Clone().Parent = workspace

Basically, the script randomly chooses one of the models in ReplicatedStorage, and clones it in Workspace.

Oh, so as @TheLegendaryDragon99 suggested you can use Clone() to clone the random child and then change it’s Parent property to workspace.

local Children = game.ReplicatedStorage:GetChildren()
local AmountOfChildren = #Children  
local RandomPositionInArray = math.random(1, AmountOfChildren)
local RandomChild = Children[RandomPositionInArray] 

local Clone = RandomChild:Clone()
Clone.Parent = game.Workspace
2 Likes

something’s wrong with the line : local RandomPositionInArray = math.Random(1, AmountOfChildren) (attempt to call a nil value)

Oops, I did math.Random I meant to do math.random() the r is meant to be lowercase