I’m working on a game where I’d like to make the world as immersive as possible, so at night I’d like to turn on street lights and lock all the shop doors via garage-like doors in a large city.
The most obvious way is to make a script that would turn on the light/change the location of the Script.Parent and then copy paste the part around. This, of course, would cause a big amount of lag.
Is there a more efficient way to achieve what I’m trying to do?
You could have a local script in StarterPlayerScripts that goes through all the lights in a loop and edits their property instead of copy pasting the script.Parent method
Just expanding upon and showing an example of what @DataSigh said:
They are likely referring to using a for loop, which you can use to go through every object in the workspace. You can check if it’s any of the three light objects, then run code for that object in specific.
for i, light in pairs(workspace:GetDescendants()) do --// Loops through every object in the workspace, the children of those objects, the children of those objects, etc.
if light:IsA("PointLight") or light:IsA("SpotLight") or light:IsA("SurfaceLight") then --// checks if the current the loop is on is a light
--// Change the properties of the light you want here using "light" from the for loop
else
continue --// If the current object the loop is on is not a light, it will continue to the next object
end
end
There isnt really any efficient ways to Handle what you want, since you would be changing potentially hundreds on things a Second, causing lag.
GetChildren is inefficient. And GetDescendants is worse, along with @AmoraFolf having a very inefficient if statement, which could be shortened to just checking for a single base class. Every Lighting related Instance Inherits a Class, with the main class for everything being Instance, for example, a Parts Base class is BasePart, but the Class it descends from is PVInstance which stands for Position Velocity Instance.
The best and most efficient thing you can do is use for this type of system currently is CollectionService to Tag all the Light Sources into one tag, using this Service, you can easily grab what you need, and edit them.
You dont need to aimlessly look for something when you can grab it directly whenever you like.
The best and most efficient thing you can do is use for this type of system currently is CollectionService to Tag all the Light Sources into one tag, using this Service, you can easily grab what you need, and edit them.
Could you please elaborate more on this - like how could that be done? It’s my first time even hearing about tags in rblx.
local lightTag = "ToggleLight"
function toggleLights(state)
local taggedLights = CollectionService:GetTagged(lightTag)
for _, light in ipairs(taggedLights) do
light.Enabled = state
end
end
placing whatever tag you choose on your lightings and then placing the function on your day night cycle, i didnt do any checks for item type to increase preformance speeds, you can also wrap it on coroutines to devide the work load if you have many lights, altough testing with around 500 i only got 0.3% script activity
I have to agree with the above, about using Collection Service to get all the objects.
Also, if you need to move a lot of parts, maybe look at the documentation on CFrames, the part about bulk cframe updates.
However, handle this on the client if possible. The server does not need to handle turning on a frivolous amount of lights - the client can do this easily without wasting the server’s processing time.
I third using CollectionService. If you need to turn on only specific lights, this data can be sent over the RemoteEvent responsible for communicating light controls (if you even need one). Just tag all lights and then control their status on the client.
Same deal for moving parts. Use TweenService or animations. Do them on the client, and on the server simply block off the areas that are no longer accessible.