Return only returning one thing

I made a simple script that clones all the workspace’s children, parents them to a viewport frame, and then deletes them. I tried to use run service, but it was to quick for a wait(), and just used a loop. However, I had an idea that how about I make the function return the clone, that way when the loop gets it, it deletes it, syncing the two together without two different waits. But, it only returns one thing. It shows one thing, prints one thing. I don’t know why.

local surfacegui = script.SurfaceGui
local viewportframe = surfacegui.ViewportFrame
local worldmodel = viewportframe.WorldModel
local runservice = game:GetService("RunService")

viewportframe.CurrentCamera = workspace.CurrentCamera

local function render()
	local children = workspace:GetChildren()
	for i,v in pairs(workspace:GetChildren()) do
		if not v:IsA("Terrain") and not v:IsA("Camera") then
			local clone = v:Clone()
			if clone then
				clone.Parent = worldmodel
				wait()
				--return clone
				clone:Destroy()
			end
		end
	end
end

--runservice.RenderStepped:Connect(function()
--	coroutine.wrap(function()
--		local c = render()
--		c:Destroy()
--	end)()
--end)


while wait() do
	coroutine.wrap(function()
		render()
		--local c = render()
		--c:Destroy()
	end)()
end

You are setting the CurrentCamera of the viewportframe to the player’s camera which won’t show the player

viewportframe.CurrentCamera = workspace.CurrentCamera

Change this part

I see nowhere whete it returns anything

ok thats fine but it doesn’t have to do anything with it not returning the other children

my bad, I deleted the part where it prints it. You can see in the first time I test the game, it only shows one part which is the issue. The second time, I get rid of the return thing and just use two waits.

There is only the part holding the frame, the player, the baseplate and the other part, baseplate is too big to be displayed, add another part, it will show up

no, the other half of the video shows the baseplate, also because the current camera is my camera, it also shows the monitor part

And in the second half everything was shown? So whats wrong? ;-;

well I eventually want to use render stepped, and the wait() isn’t instant, so it ends up being hundreds of parts and baseplates in the viewport frame, becuase its too slow to delte the parts. So I had an idea, make the function return the cloned part, that way every render stepped it clones it, then as soon as it gets the clone, it deletes it

oh yeah and also when I use two waits you can see the children stuttering/ disappearing from the viewport frame which wouldnt happen if the wait is fast enough

I didnt quit understand but with renderStepped no need to have a wait() roblox handles that part by itself

I didnt mean put a wait inside the render stepped I meant

from what i see in the script, you loop through workspace’s children, but return after the first iteration, and then repeat this process every frame, so you only end up cloning a single part

although the only time your function returns is commented out so i dont know if you have changed it

in the render function you should just parent every clone to a single folder and then return the folder

It returns one child since when using return it will just stop the whole function and return that item

1 Like

oh uh well I jus tried that, but umm, it still stopped the function.

local function render()
	local children = workspace:GetChildren()
	for i,v in pairs(workspace:GetChildren()) do
		if not v:IsA("Terrain") and not v:IsA("Camera") then
			local folder = Instance.new("Folder")
			folder.Parent = worldmodel
			local clone = v:Clone()
			if clone then
				clone.Parent = folder
				--wait()
				--return clone
				--clone:Destroy()
			end
			return folder
		end
	end
end



while wait() do
	coroutine.wrap(function()
		--render()
		local folder = render()
		--wait()
		folder:Destroy()
	end)()
end

you would have to delcare the folder before the loop and return it after the loop is finished otherwise youll only end up putting one item in the folder

1 Like