So, to make things clear, I have multiple parts on the roof of my game map game and every single one of them has a spotlight instance in, with the brightness attribute set to 5. And I have all of these parts into a model. And I often rely on short ways to solve a problem. And basically, I’m making a script into that model that takes every part into the model and sets their spotlight brightness to 1 and then after 1 second back at 5.And I want all the parts to do it, at the same time. But I don’t know how to implement this algorithm. I think that spawn() and delay() is the solution or not. Let me know it that is the case, or not, or I need to manually insert a flickering script into every part of the model. Any opinion will appreciated.
one lazy way to do it is to make them all set their brightness to a number value in workspace constantly (or when the value changes), and then flicker the number value between 5 and 1 to get them all to flicker at the same time
Put just the Parts with Spotlights in a folder or a model.
In your script use a loop to get pairs or ipairs and change the Brightness in the loop.
You can also set the Brightness value as a IntValue in the loop so you only have to write that section of code once. Just change the IntValue to 1 or 5 then run the loop to make the lights bright or dim.
I what them to flicker at the same time, the method you are telling me works with only taking each of the parts in order and change their spothlights. But i want all of them to flicker at the same time.
Yes, you’ll need to use task.spawn to make separate threads so that it doesn’t yield. Which leads into flickering without being yielded.
How can I implement this?
Are are for loops and while loops needed in here?
It should run all of them in the same millisecond, so it most likely won’t be noticeable. If you’re really dead set on not yielding, use task.spawn in a for loop.
Example:
for _, light in lights do
task.spawn(function()
brightness = 0
task.wait()
brightness = 5
end)
end
This is pseudocode but you get the idea.
To make things clear once and for all this is the model I’m talking about:
I modified your pseudo-code as I needed.
The result looks like this:
for light in pairs(script.Parent:GetChildren()) do
task.spawn(function()
light.SpotLight.Brightness = 1
task.wait(0.5)
light.SpotLight.Brightness = 5
end)
end
But when I was testing I encountered this error:
How can I make things better?
You have to have the _,
at the beginning. That is the index, I just made it an underscore since it isn’t used. The second value, light
, is the actual part itself.
Ok, fixed the part with the underscore. But I don’t know how to solve the SpotLight thing in the output it gives me the error that it isn’t a valid member of workspace.Model.Part, but the SpotLight really is a member of every part in the model…
I can’t figure out what is the issue.
Can you send a screenshot of the explorer with the parts expanded and the output? This is a separate issue in itself but you’ll also need to make sure the object looped through is a part so it doesn’t check inside the script
So, basically the model in the explorer is just a regular model, with children that are called part, part, part, and then a simple script.
But the output looks like this:
Yeah but can you send a screenshot of some of the parts expanded? Is the light actually named SpotLight?
Out of curiosity, can you print the children of the part in the for loop before you attempt to change its light and see what it does?
Shouldn’t the index have been increasing?
That looks exactly as it should. The index shouldn’t be increasing since it’s each different part’s children. I just tested this myself and it works exactly how it should, with a check to make sure it’s a part. Can you send your exact script currently? I don’t see how this is happening
Edit: Try doing a WaitForChild, that’s a bandaid fix but it might give some insight
for _,light in pairs(script.Parent:GetChildren()) do
print(light:GetChildren())
task.spawn(function()
light.SpotLight.Brightness = 1
task.wait(0.5)
light.SpotLight.Brightness = 5
end)
end
Here it is.
That’s so weird. Can you try using WaitForChild?
Where should i use WaitForChild?
light:WaitForChild("SpotLight").Brightness = 5
task.wait(0.5)
light:WaitForChild("SpotLight").Brightness = 1
Here?