Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make this code do the same for each child instead of moving each one individually?

I'm making a script that moves parts in a model in serverstorage to a model in workspace, here's my code;

local active = script.Parent.active
local donutfolder = game.ServerStorage.donuts
local donutcode = script.Parent.donutcode
local donuts = script.Parent.donuts

while true do --> Looks a bit messy but it gets the job done.
    if active.Value then
	else
            donuts:ClearAllChildren()
			end
        if donutcode.Value == 1 then
	
           donut.Parent = donuts --> So I tried a for i, donut in pairs(donutfoler.D1:GetChildren()) do but that didn't work, but can I modify it so it works? 
		donut.Anchored = false
        end 
wait(5)
end

So if I add a for i, donut in pairs(donutfoler.D1:GetChildren()) do but it broke the script, how do I fix this? And no, I wasn't getting any errors whatsoever.

2 comments

0

just parent it into workspace?? why do u need to individually parent the children, just parent the model/whatever

0

Thats not whats looking to be achieved.

1 answer

Your code is really messy and screwed up so I went ahead and re-wrote it.

This has to be in a script, not a localscript, since localscript cannot access ServerStorage or ServerScriptService.

local active = script.Parent.active
local donutfolder = game.ServerStorage.donuts
local donutcode = script.Parent.donutcode
local donuts = script.Parent.donuts

-- Its common practice to avoid using wait(), as it is described as a "Code smell"

game:GetService("RunService").Heartbeat:Connect() 
	if not active.Value then
		donuts:ClearAllChildren()
	end
	if donutcode.Value == 1 then
		for _,donut in pairs(donutfolder:GetChildren())do
			donut.Parent = donuts
			donut.Anchored = true
		end
	end
end

Comment any errors or questions.

This discussion is preserved as it was. New answers, comments, and votes are closed.