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

How to fire a function (for i,v in pairs) for two models at once?

Asked by 4 years ago
Edited 4 years ago

Okay! I've remade this post about two times and was asked to be more descriptive. What I'm trying to do is have the function work for both models at the same time if that makes sense. I'm not sure if I properly set it up for it to do that. I don't really know how to explain it! I'm REALLY new to scripting, that's why I'm here lol.

The script:

function onClick()
for i,v in pairs (game.Workspace.MLights:GetChildren()) or (game.Workspace.MSLights:GetChildren())
do
v.Part.Transparency

1 answer

Log in to vote
0
Answered by
Nowaha 459 Moderation Voter
4 years ago
Edited 4 years ago

If you don't understand for loops, this is how they work:

They loop over all the values inside of a dictionary or array.

The i is the index the object has in the array, in this case. The v is the object that is at that index in the array.

for index, object in pairs(array) do
    -- Code in here will execute for every object inside of the array.
end

Based on the code in your question I assume you are trying to set the transparency of all the parts inside of the MLights object. This is how you would do that:

for index, object in pairs(game.Workspace.MLights:GetChildren()) do
    -- Code in here will execute for every object inside of the array.
    -- object is the child. I don't know if you're trying to get the part INSIDE of the object.
    -- If you are, use this:
    object.Part.Transparency = 1
    -- If you are not, object is already the part.
    object.Transparency = 1
end

You can then short index or object to i, v if you want to.

Refer to this article if you need more help: https://developer.roblox.com/en-us/articles/For-Loops

Ad

Answer this question