How to make clones from a table

So I’ve been following a tutorial but changing a lot of things for my own game, and right now I’m working on respawn-able ores. The tutorial uses one rock type, where-as I use multiple, meaning our scripts have to be different, and sometimes (for example now) I don’t know what to change.

Heres my current code:

local rocks = script.Parent:GetChildren()

local rockLocations = game.Workspace.Harvestables.OrePos:GetChildren()
local numLocations = #rockLocations

for place = 1, numLocations - 100 do
	local rockClone = rocks:Clone()
	rockClone.Parent = script.Parent
	rockClone.Position = rockLocations[place].Position
	rockLocations[place].Free.Value = false
end

rocks:Destroy()

So far (as you can see one solution already) I’ve tried to use get children on the rocks variable, though that hasn’t worked, and I don’t know what else to change to make it compatible with the table.

1 Like

Maybe there isn’t more than 100 rock locations? If there is 100 or less rock locations, there would be no object to index in the rocks children (because it is 0 or less). You can try to use the numLocations instead of subtracting it by 100.

Script
local rocks = script.Parent:GetChildren()

local rockLocations = game.Workspace.Harvestables.OrePos:GetChildren()
local numLocations = #rockLocations

for place = 1, numLocations do
	local rockClone = rocks:Clone()
	rockClone.Parent = script.Parent
	rockClone.Position = rockLocations[place].Position
	rockLocations[place].Free.Value = false
end

rocks:Destroy()

I’m not very sure how your game is arranged, so you would have to give out more information to get proper assistance.

sorry, heres a photo of where I want the rocks to respawn and be located. I’d rather it be more open, which is why I picked 100, though that seems kind of small.
Each “rock” is 4x4x4 in size, so I put a 4x4x4 part to the right for scale

1 Like

After getting the children of any object, it will return a table with their children inside of it. You cannot clone tables with :Clone(). I’m assuming you were trying to clone all of the old children inside of the rocks folder and parent it into the rock folder? After that, you would destroy the old children.

I’m still not sure where you were going with the numLocations - 100, but I changed the loop around to only run if the place variable is more than 100. It will subtract 100 from the place variable and index the rockLocations table with that number. After cloning the rocks, the old rocks will be destroyed.

Script
local rocks = script.Parent
local rocksChildren = rocks:GetChildren()

local rockLocations = game.Workspace.Harvestables.OrePos:GetChildren()
local numLocations = #rockLocations

for place = 1, numLocations do
	if place <= 100 then continue end
	local rockIndex = place - 100
	for _, rockPart in pairs(rocksChildren) do
		local rockClone = rockPart:Clone()
		rockClone.Parent = rocks
		rockClone.Position = rockLocations[rockIndex].Position
	end
	rockLocations[rockIndex].Free.Value = false
end

for _, rockPart in pairs(rocksChildren) do
	rockPart:Destroy()
end
1 Like

For some clarification, the numLocations - 100 is how many spaces to leave, for example if it was 1, then there would be 1 open space/free space, and I cant really tell if the place variable is the same thing in that script as well.

1 Like

So… for instance, one open space for every taken space? But in this case, it would be every 100 open spaces is 100 taken spaces, or just 100 open spaces and the rest taken?

1 Like

I’m not entirely sure what you mean by that, but basically what I mean is in a nutshell, numLocations is the total possible locations for rocks to spawn, and without the - 1 in my script, every spawn would take up the entire mining platform. the - 1 is more like total free spaces, so if you wanted there to be 28 free spaces total out of however many you have, you would want to do numLocations - 28

1 Like

Oh and also I tested your script, and it almost worked besides putting every single rock into the same position

1 Like

So, I’ve made it so your script has 100 free spaces. What the script will do is clone a random rock from the rocksChildren, and it will be placed in a random position inside of the rockLocations. Also, if you have multiple types of rocks, I’m not very sure if you can clone every single one every time you are respawning the ores, because you have 100 free spaces. I’m not sure though, but you could make random ores appear when you’re mining (I assume you mine in the game). Once the free spaces run out, it will stop cloning the objects and break the loop.

Script
local rocks = script.Parent
local rocksChildren = rocks:GetChildren()

local rockLocations = game.Workspace.Harvestables.OrePos:GetChildren()
local numLocations = #rockLocations

local freeSpaces = 100

for place = 1, numLocations do
	local rockPart = rocksChildren[math.random(1, #rocksChildren)]:Clone()
	if freeSpaces > 0 then
		rockPart.Parent = rocks
		local randomLocation = rockLocations[math.random(1, #numLocations)]
		rockPart.Position = randomLocation.Position
		randomLocation.Free.Value = false
		freeSpaces -= 1
	else
		rockPart:Destroy()
		break
	end
end

for _, rockPart in pairs(rocksChildren) do
	rockPart:Destroy()
end
1 Like

Alright so I tried your script and it didnt work, however I have an idea that takes part of your script and part of the original script, and puts them together, so unless you have any other suggestions, should I tell you if it works?

1 Like

Sure, you can combine the two scripts.

1 Like