Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Cloning random object then moving it to the parent?

Asked by 5 years ago

I have a part created that basically a small plate, ontop of it I want to spawn a random ore from a list of ores in a folder. I have tried using math.random but when using script.Parent is was teleporting multiple ores to a single plate. (I want to be able to copy and paste this plate around the map instead of having to edit the code for each individual spawn-plate.

Original Script:

local rocks = game.ReplicatedStorage.ores
local spawner = script.Parent
local rockclone1 = game.ReplicatedStorage.ores.Rock1:Clone()
local rockclone2 = game.ReplicatedStorage.ores.Rock2:Clone()
local rockclone3 = game.ReplicatedStorage.ores.Rock3:Clone()
local rockclone4 = game.ReplicatedStorage.ores.Rock4:Clone()
local rockclone5 = game.ReplicatedStorage.ores.Rock5:Clone()

local gen = math.random(1,5)

    if gen = 1 then
        rockclone1.Parent = spawner
        rockclone1.Position = spawner.Position
    else if gen = 2 then
        rockclone2.Parent = spawner
        rockclone2.Position = spawner.Position      
    else if gen = 3 then
        rockclone3.Parent = spawner
        rockclone3.Position = spawner.Position      
    else if gen = 4 then
        rockclone4.Parent = spawner
        rockclone4.Position = spawner.Position      
    else if gen = 5 then
        rockclone5.Parent = spawner
        rockclone5.Position = spawner.Position          
    end

I realized this was kinda messy and i'm still new to lua so I did a little bit of research and tried to simplify the random code using Random:Clone()

New code:

local rocks = game.ReplicatedStorage.ores
local spawner = script.Parent
local rockclone1 = game.ReplicatedStorage.ores. Random:Clone()

rockclone1.Parent = spawner
rockclone1.Position = spawner.Position

With this second code I keep getting a repeat error where it's telling me that random isn't a member of folder, the first script gives me a "expected then near =" error I had it working before but when it was working it only worked if I only had one placed within the world.

I essentially just want ores to spawn and regen every 5 minutes drawing a random model from the designated folder. I'm not sure how to go about this without the script confusing the different objects, the first one parented correctly originally but then it was moving objects to the location of the wrong script.

1 answer

Log in to vote
0
Answered by 5 years ago

Fiddled around and figured it out, posting here just in case anyone else needs the solution:

local list = game:GetService("ReplicatedStorage").ores:GetChildren()
local oregen = ( list[math.random(#list)] ):Clone()

oregen.Parent = script.Parent
oregen.Position = oregen.Parent.Position
oregen.Parent.Transparency = 1
Ad

Answer this question