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

How do you make a for loop that checks inside a certain place?

I'm trying to make a for loop that checks each item in server storage.

This is what I've tries so far.

for i in serverstorage do
	if i.Starter then
		local item = i:clone()
			i.Parent = player.PlayerGui
	end
end

2 answers

✓ Accepted answer
local serverstorage = game:GetService("ServerStorage") -- Gets the ServerStorage

for i, child in pairs(serverstorage:GetChildren()) do -- Creates a table of the children in serverstorage
  if child:FindFirstChild("Starter") then -- Using FindFirstChild stops the annoying error
    local item = child:Clone()
   item.Parent = player.PlayerGui
 end
end

Edit: Thanks to incapazz for pointing out that you likely are using a local script. In that case just change ServerStorage to ReplicatedStorage. If not, then ServerStorage is fine.

1 comment

0

Thanks, this works

Since you’re using PlayerGui, I’m assuming this is a local script. ServerStorage doesn’t exist on the client. Instead use ReplicatedStorage and it’s :Clone not :clone.

for_, v in pairs(game:GetService('ReplicatedStorage'):GetChildren()) do
    if v:FindFirstChild'Starter' then
        local item = v:Clone() -- Clone not clone
            item.Parent = player:WaitForChild"PlayerGui"
    end 
end

2 comments

0

it is :Clone() not :clone or :Clone

0

same thing

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