Moving multiple models from a folder, to another folder at once

First of all, hello! Ummm I’m recently working on an elevator game to test my skills in general.
And I’m trying to move the floors that are on the “Workspace” to another folder that is on the “Lighting” whenever the math.random selects a random floor.
I hope that was clear. I’m looking forward for your answers.

Here’s the test code if you want to see:
It somehow doesn’t work.

local folder = game.Lighting.Floors
local Children = game.workspace.Floors:GetChildren()

while true do
	local random = math.random(1,3)
	
	if random == 1 then
		for i = 1, #Children do
			Children[i].Parent = folder
		end
		folder.Floor1.Parent = workspace.Floors
		random = 0
		
	elseif random == 2 then
		for i = 1, #Children do
			Children[i].Parent = folder
		end
		folder.Floor2.Parent = workspace.Floors
		random = 0
		
	elseif random == 3 then
		for i = 1, #Children do
			Children[i].Parent = folder
		end
		folder.Floor3.Parent = workspace.Floors
		random = 0
		
	end
	
	wait(10)
	
end

You could delete all those elseif’s and just put

for i = 1, #Children do
	Children[i].Parent = folder
end
folder.["Floor"..random].Parent = workspace.Floors
random = 0
1 Like

Try replacing game.workspace with workspace (no game.)).

I don’t know if this contributes to this discussion , but line 2, ‘workspace’ has a lowercase w while it should be a uppercase.

It technically works but It seems like the first 3 lines aren’t working as intended.

what I’m trying to do is, whenever the loop triggers, the for loop has to clear the game.Workspace.Floors folder by moving all the models inside of it (Even if there’s just one) to the game.Lighting.Floors aka folder (variable)
And then the script should select a random model from game.Lighting.Floors and send it to game.Workspace.Floors.
Hope that was clear. It was hard to write lol.

I don’t think it was the solution but I appreciate the effort. thx for letting me know :slightly_smiling_face:

Maybe use :GetDescendants() instead of :GetChildren()? Please explain what you expect to happen vs what is really happening, both parts are very important to helping.

This should work, and the comments should explain it

local workspaceFloors = game.Workspace.Floors -- the folder in the workspace with the folders
local lightingFloors = game.Lighting.Floors -- the folder in lighting which you want to move them to

function chooseFloor()
	local floors = workspaceFloors:GetChildren() -- a table of all the floors
	local num = math.random(1, #floors) -- randomly selected number in that table
	local floor = floors[num] -- get the floor frfom that table
	
	floor.Parent = lightingFloors -- set the selected floor to the lighting folder
end


chooseFloor() -- runs the functions

explorer:
2022-06-21 12_03_08-Script - Roblox Studio

1 Like

This will generate a random number every 10 seconds, then loop for every floor in the workspace floor folders, and if the name is for example Floor9, Floor3 or whatever number chosen it will put it into lighting, is this what you meant?

local floors = game.Workspace.Floors:GetChildren()

local function RandomFloor()
    local num = math.random(1,#floors)
    local floorName = "Floor"..num

    for i = 1, #floors do
        if floors[i].Name == floorName then
            floors[i].Parent = game.Lighting.Floors
        end
    end

    return floorName

end

while true do wait(10)
    
if RandomFloor() == "Floor1" then
-- code here
elseif RandomFloor() == "Floor2" then
-- code here
elseif RandomFloor() == "Floor3" then
-- code here
end -- repeat this for all the floors

end

“what I’m trying to do is, whenever the loop triggers, the for loop has to clear the game.Workspace.Floors folder by moving all the models inside of it (Even if there’s just one) to the game.Lighting.Floors aka folder (variable)
And then the script should select a random model from game.Lighting.Floors and send it to game.Workspace.Floors .
Hope that was clear. It was hard to write lol.”

It does move the folders from lighting to workspace but it doesn’t move them back to lighting.

Depending on how your workspace is set up you might need to update your children variable more often. It would probably be best to never store it as a variable, unless your workspace.Floors starts with every floor in it.

The following code reduces variables usage so it may more accurately reflect the workspace at every point in time. Also as others have suggested using string manipulation to remove long if/elseif chains.

local folder = game.Lighting.Floors

while true do
    for _, child in ipairs(workspace.Floors:GetChildren()) do
        child.Parent = folder
    end
    folder["Floor" .. math.random(1,3)].Parent = workspace.Floors

    task.wait(10)
end

I found a way to mix your code with @gertkeno and @Ryanogic’s code and somehow it works!

Here’s the code and Idk whose reply should I tag as the solution…
And I know this might not be the best way to do it but as long as it works as intended I’m okay about it.

(I tagged @gertkeno’s code as the solution. By far it looks like it’s the best one that could do the entire task on itself)

local workspaceFloors = game.Workspace.Floors
local lightingFloors = game.Lighting.Floors 
local floors = game.Workspace.Floors:GetChildren()
local NumberOfFloors = 3

function sendFloors()
	
	for i = 0, 1, .2 do
		for i, v in pairs(workspaceFloors:GetChildren()) do
			if v:isA("Model") then
				v.Parent = lightingFloors

			end
		end
	end
	
end

while wait() do
	
	local random = math.random(1, NumberOfFloors)

	if random == 1 then
		lightingFloors.Floor1.Parent = workspaceFloors
		random = 0

	elseif random == 2 then
		lightingFloors.Floor2.Parent = workspaceFloors
		random = 0

	elseif random == 3 then
		lightingFloors.Floor3.Parent = workspaceFloors
		random = 0
	end
	
	wait(10)
	
	sendFloors()
	
end

EDIT: Changed something about the code.

1 Like

If this code works them I’m pretty sure your issues are because of the children as a variable. This code will fail to return any floors that are in the lighting folder when you start. Are you sure this works after the second or third floor?

1 Like

Hold on… There has to be an issue! You are right! I’ll test it out and fix it immediately

Well umm basically this one works but this one has if statements. Which was a thing that I wanted the code to contain at the first place for events that will only occur when a specific floor is chosen etc.

local workspaceFloors = game.Workspace.Floors
local lightingFloors = game.Lighting.Floors 
local floors = game.Workspace.Floors:GetChildren()
local NumberOfFloors = 3

function sendFloors()
	
	for i = 0, 1, .2 do
		for i, v in pairs(workspaceFloors:GetChildren()) do
			if v:isA("Model") then
				v.Parent = lightingFloors

			end
		end
	end
	
end

while wait() do
	
	local random = math.random(1, NumberOfFloors)

	if random == 1 then
		lightingFloors.Floor1.Parent = workspaceFloors
		random = 0

	elseif random == 2 then
		lightingFloors.Floor2.Parent = workspaceFloors
		random = 0

	elseif random == 3 then
		lightingFloors.Floor3.Parent = workspaceFloors
		random = 0
	end
	
	wait(10)
	
	sendFloors()
	
end

I see your still replying here, so if your still having issues I changed my response, I have no idea how I messed up that bad but I was tired, try what I did I guess if you are still having issues.

Tell me if it works.

Does this example not work? I think your latest response working is because your using :GetChildren() directly instead of saving a variable, please try this code and tell me how it goes.

local folder = game.Lighting.Floors

while true do
    -- put ALL floors away (usually only 1)
    for _, child in ipairs(workspace.Floors:GetChildren()) do
        child.Parent = folder
    end
    -- parent a random floor to the workspace
    folder["Floor" .. math.random(1,3)].Parent = workspace.Floors
    -- if only floors are expected in the folder this could work too
    --folder:GetChildren()[math.random(1,3)].Parent = workspace.Floors

    task.wait(10)
end

Alrighty I’m going to try that. Sorry I fell asleep. It took me a while to respond :smiling_face_with_tear:
The reason it puts all the floors away is because if something happens and if it somehow skips while moving the model back to Lighting, It would be a problem. So Just in case it moves all the models back.

Hi! Alright I’ll try your script to see if it works, and don’t worry we all mess up… At the end of the day we are just humans.

It seems like it’s working well! Yeah, I think I made a big mistake by using :GetChildren() inside a variable.
Now I realize… It looks like script would always skip the for loops. Probably because of the :GetChildren() problem that we had. I tried putting a print inside one of the older for loops that aren’t working, It didn’t print it out.