Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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.

0
Thanks, this works ninja_eaglegamer 61 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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
0
it is :Clone() not :clone or :Clone valchip 789 — 5y
0
same thing User#19524 175 — 5y

Answer this question