Is there a way to loop check parts?

Hey! I am making a script where when a button is pressed all of the lights turn on and the parts change material to neon. Is there a way to change everything at once so i dont have to make a variable for each of the parts? if so please let me know of a better way to do this…

2 Likes

Yes! But it’s hard to tell you exactly how without knowing how your Workspace is organized.

If all the lights are in a Folder or Model, you can loop through each like so:

for _, light in pairs(game.Workspace.Lights:GetChildren())

end

If the lights are in all sorts of different things, it might be easier to use a Tag/CollectionService plugin to mark each light with a specific tag, and then loop through them like this:

for _, light in pairs(CollectionService:GetTagged("Light")) do
    
end
4 Likes

Thank you! all of the lights are in a folder so that will help!

I do have one question though how would i define all of the parts? would i say part.material = concrete? or something else?

1 Like

You look under the Part wiki page, scroll down to properties and check out Material. You can click on each property to get more details, like this: BasePart | Documentation - Roblox Creator Hub

There are examples and stuff, as well as a Value Type, showing which kind of value you can set Material to. In this case it’s an Enum, so you can do part.Material = Enum.Material.Grass or part.Material = "Grass". Both work the same.

1 Like

Thank You! so much for your help!

You can make a RemoteEvent then in the client use this code

game.ReplicatedStorage:WaitForChild("REMOTEEVENTNAME").OnClientEvent:Connect(function()
-- do the light loop here
end)

when you want to turn on or off the lights just fire the remote event and when players receive the instruction it will be done as fast as possible, no waiting for server to replicate.

1 Like