I have a lot of objects with the same script, so say I wanted to update that script, I cant go one by one because that would take too long. Right now my solution is to make a new script, search for the name of the part that has the script I want to update (luckily I labled the parts so all of them showed up), delete the existing script inside those parts, and then past the new script into the parts. Is where a way to have a way to massive update the same script? Like if I have the exact same script across a bunch of objects, is there a way to make it so if I update one it updates all?
#is there a way to have some sort of master script>?
226 messages · Page 1 of 1 (latest)
** You are now Level 1! **
What i do is make a for loop that handles every object, its only one script and you just wouldnt have to ever worry about mass updating your objects. Ive also heard people saying doing it the way i just mentioned is also alot more optimized. So for example:
for Number, Descendant in pairs(workspace:GetDescendants()) do
if Descendant.Name == "name of whatever object your tryna do stuff with" then
-- blah blah blah code here
end
end
You could also use collection service if you wanted to be even more orginized but i just use a simple for loop (Keep in mind you should probs update to your own likeing cuz the current script assumes the objects your trying to script are a direct descendant of workspace)
so for example if I had a modul to which I want to apply this with would I simply replace workspace:GetDescendants() with module:GetDecendants()?
assuming the module is a direct ancestor of the objects yes
do you understand for loops?
No I don't really, I know how they work but I haven't really learned them on lua, right now I'm working on the building part of my game while I try to learn Lua on the side so I have an idea of how to get it working once I finish the building part of the game.
Ahh i see, alls the loop is doing is iterating (going) over each descendant to see if its name is the name of the object that you want to do stuff with, just thought i would provide you with a lil context so you get the jist of what its doing
also dont be calling the for loop every single second cuz it can get laggy if your spamming them
Yea I get that, tbh for loops have always been the most confusing for me, rn my biggest thing in lua programming is the syntax itself, I'm getting the hang of it but since my previous knowledge in programming was already small, switching languages has only been easy bc I'm still in the basics lmao
what launguage did you start with?
yea I'm going to see how I can optomize my scripts so they dont cause lag, the thing is i have alot of lights rn which I scripted to flicker so since theres so many I feel like itll be bad eventually
python
also for loops in lua are relitivly easy compared to alot of other launguages, you only really use them for tables and hierarchial stuff
but i didnt get pass learning aboutn _innit_
oh so you got to like object orientated stuff
lua does some oop stuff but its far from required just makes stuff a lil easier
I think so, im not sure but I think I remember seing that at some point and working with it
if your coming from python your already in relitivly good shape, i started off with lua with no prev knowledge and it took ages to learn lmao
yea loops are useful, im just going to hope i dont make a lot of lag, right now im still working on the first building and it already has so many parts and scripts
yea lmao i struggled a lot with learning lua since i didnt know any of the names used fro stuff like humanoid and all that
thats the roblox side of it lol they just adapted lua to fit their engine
memorizing all the services and names and spelling takes a while
yea so far I only really know how to do a few things but so far I think I have enough knowledge to at least get me through this game since I don't plan on making anything too complex just a bit of gui which I'll have to figure out later
Ui is really beginner friendly
thats what i really started off with
3 lines of code to make a really simple button work lmao
yea I hope its easier than building, bc building for some reason seems a lot harder, ive spent about a day now on a single side of the building
also you mentioned how scripts cause lag
I have a lot of lights and each of them has a script in them to make the ligh flicker at random times, would that cause a lot of lag?
it uses a while loop
** You are now Level 2! **
honestly the for loop might allevaite some of the lag a little bit, but in general having alot of loops might cause a tiny bit of lag dependant on if its client or server sided
how would I know the difference?
is just put like script and then named it and put some code in it
is it a script or a local script
script
really depends on the players machine power (cpu/gpu ext)
are you putting it on mobile?
because standard computers would do that many loops with ease
I dont think so, it would'nt be fun to play on mobile since its kinda battle oriented
heres how i would deal with the lag, i would only make the lights close to the player flicker
and keep the for loop
actually now that you mention it if your doing loops inside the for loop i gotta give you and updated script
for Number, Descendant in pairs(workspace:GetDescendants()) do
if Descendant.Name == "name of whatever object your tryna do stuff with" then
task.spawn(function())
-- code here
end)
end
end
task.spawn is creating a new thread for each loop
i was using the script to lock things in studio so I wouldnt have to accidentally click something then lock it and then end up needing to click it later and havung to unlock
the script that im worried about causing lag is this one
local function Flicker()
local flicktime = math.random(0.9,1)
local startflick = math.random(10,40)
local secondflicktime = math.random(0.9,1)
wait(startflick)
script.Parent.SpotLight.Enabled = false
wait(flicktime)
script.Parent.SpotLight.Enabled = true
wait(secondflicktime)
script.Parent.SpotLight.Enabled = false
wait(flicktime)
script.Parent.SpotLight.Enabled = true
end
while true do
Flicker()
end
idk if it would help but if i did something like this instead i wonder if it would help
local function Flicker()
local flicktime = math.random(0.9,1)
local secondflicktime = math.random(0.9,1)
wait(startflick)
script.Parent.SpotLight.Enabled = false
wait(flicktime)
script.Parent.SpotLight.Enabled = true
wait(secondflicktime)
script.Parent.SpotLight.Enabled = false
wait(flicktime)
script.Parent.SpotLight.Enabled = true
end
while true do
local startflick = math.random(10,40)
wait(startflick)
Flicker()
end
that way it isnt running the loop every time idk if it makes a difference or not
it honestly wouldnt and it would behave alot differently
how so?
hard to put it into words lol gimme a sec
actualy wait
aslong as you only refer to start flick once within the function it wouldnt make a difference
same with secondflicktime
you could move that out
but it fundemently doesnt really change much
hm alr
but if I had this script running on like
im not counting allat, but around 50 parts, would it be a bad idea?
honestly if your going to implement it i would make it optional and on the client side
how would I go abt that, if you don't mind explaining, I have no idea how to do that
make the for loop inside a client script within starter player scripts
i just reread this and i realized i butchered it i thought you were redefining the flicktime varible for some reason mb
if I have this script and change it a bit from script.Parent to something like workspace.module.SpotLight would it affect every module that is named the same?
like if Descendant:GetAttribute("Light") then
so you could add an attribute to each part that your want effected
OR
you could tag them with collection service
which if you dont know what attributes are they are pretty much properties but made by you and not roblox
so custom properties
oh? I'll have to work on learning that soon it seems helpful
** You are now Level 3! **
this script became a mouthful but
local function Flicker()
local flicktime = math.random(0.9,1)
local startflick = math.random(10,40)
local secondflicktime = math.random(0.9,1)
wait(startflick)
workspace.Hospital.WestBuilding.WestBuildingLights.Light.Bulb.SurfaceLight.Enabled = false
wait(flicktime)
workspace.Hospital.WestBuilding.WestBuildingLights.Light.Bulb.SurfaceLight.Enabled = true
wait(secondflicktime)
workspace.Hospital.WestBuilding.WestBuildingLights.Light.Bulb.SurfaceLight.Enabled = false
wait(flicktime)
workspace.Hospital.WestBuilding.WestBuildingLights.Light.Bulb.SurfaceLight.Enabled = true
end
while true do
Flicker()
end
very helpfull for designating what is what and other things
would applying that script to workplace affect every bulb that is in every module named "Light"?
module or model
alr
Follow up question on lag, do parts cause lag? I have a lot of parts since my game is post-apoc and theres debri I made with a bunch of blocks
usually it doesnt unless there is 100's of thousands
but if there unachored then that will cause extreme lag
yea im making sure to have them all anchored lmao
-- Varibles
local WestBuildingLights = nil -- just define this and any other model that contains lights
-- Local Functions
local function FlickerLight(Descendant : Light)
while task.wait() do
local FLICK_START = math.random(10, 40)
local FLICK_TIME = math.random(9, 10) * 0.1
local FLICK_SECOND_TIME = math.random(9, 10) * 0.1
task.wait(FLICK_START)
Descendant.Enabled = false
task.wait(FLICK_TIME)
Descendant.Enabled = true
task.wait(FLICK_SECOND_TIME)
Descendant.Enabled = false
task.wait(FLICK_TIME)
Descendant.Enabled = true
end
end
local function GetObjectsFromLoopForCallback(Name : string, Search : Instance, CreateThreadForObject : boolean, UseGetDescendant : boolean, Callback : any)
local function GetDescendants()
for Number, Descendant in pairs(Search:GetDescendants()) do
if Descendant.Name == Name and CreateThreadForObject == true then
local Thread = task.spawn(function()
Callback(Descendant)
end)
else
Callback(Descendant)
end
end
end
local function GetChildren()
for Number, Child in pairs(Search:GetChildren()) do
if Child.Name == Name and CreateThreadForObject == true then
local Thread = task.spawn(function()
Callback(Child)
end)
else
Callback(Child)
end
end
end
if UseGetDescendant == true then
GetDescendants()
else
GetChildren()
end
end
GetObjectsFromLoopForCallback("Light", WestBuildingLights, true, true, FlickerLight)
i made it completely modular, if you want to only scan the the children of an object (more performant) to see if they are the correct name then just call the function multiple times with each model containing lightsources
if you wanna just scan the entire workspace you can do that true
just put UseGetDescedant to false if you want to scan only the models containing lights and set it to true if you just wanna scane the entire worksapce
Name is the name you wanna use to detect if the part is supposed to be scripted
Search is what it will search from
CreateThreadForObject, only use that if you have a callback that uses loops
Callback is the function that will be applied to each part
im hoping that works but ima test it myself rq
all those terms seem so
jesus
Say I wanted to have the script go to each bulb
uhh
like if I wanted to apply it to the bulb in this light
but i have multiple models* of the same thing
put the search as WestBuildingLights
you would just have to refrence it in the script
put UseGetDescedant to true
so is the bulb a descendant of both light model and westbuildinglights model?
or is it only a descendant of lgiht model
the script goes in StarterPlayerScripts?
bulb itself is a child of the light model but we are trying to get the light itself not the bulb
oh right right
bulb is however a descendant of westbuildinglights
put it in starterplayerscripts
local script
does that go for surfacelight aswell?
yes
actually wait im testing it an i made a error
lemme refactor
alr let me read this script over to get hopefully the understanding
most of the terms in it are new to me lmao
oh wait i guess this cant be client sided
cuz its not able to refrence certain shit
mb
ah
no worries
still kinda i think im getting sort of the concept to the solution
I just want a way to update like
im rlly just using the lights as an example bc its something I wanna try it on since i ahve so many of em but i think ima use this whole thing on other things as well
like just a way to update multiple scripts that all contain the same thing n stuff
is there a way to make a script, and then import it into other parts as a sort of self updating clone?
this is going to be a rlly weird analogy but kinda like how in microsoft excel you can make one cell have the SUM function and have it add up certain cells and it updates itself depending if one of the cells' values is changed
yeah but its not as performant as a for loop
hm
-- Varibles
local WestBuildingLights = nil
-- Local Functions
local function FlickerLight(Descendant : Light)
while task.wait() do
local FLICK_START = math.random(10, 40)
local FLICK_TIME = math.random(9, 10) * 0.1
local FLICK_SECOND_TIME = math.random(9, 10) * 0.1
task.wait(FLICK_START)
Descendant.Enabled = false
task.wait(FLICK_TIME)
Descendant.Enabled = true
task.wait(FLICK_SECOND_TIME)
Descendant.Enabled = false
task.wait(FLICK_TIME)
Descendant.Enabled = true
end
end
local function GetObjectsFromLoopForCallback(Name : string, Search : Instance, CreateThreadForObject : boolean, UseGetDescendant : boolean, Callback : any)
local function GetDescendants()
for Number, Descendant in pairs(Search:GetDescendants()) do
if Descendant.Name == Name then
if CreateThreadForObject == true then
local Thread = task.spawn(function()
print("Thread desc")
Callback(Descendant)
end)
else
print("desc")
Callback(Descendant)
end
end
end
end
local function GetChildren()
for Number, Child in pairs(Search:GetChildren()) do
if Child.Name == Name then
if CreateThreadForObject == true then
local Thread = task.spawn(function()
print("Thread child")
Callback(Child)
end)
else
print("child")
Callback(Child)
end
end
end
end
if UseGetDescendant == true then
GetDescendants()
else
GetChildren()
end
end
GetObjectsFromLoopForCallback("SurfaceLight", WestBuildingLights, true, true, FlickerLight)
alr i got it working
all lights in the west buildings lights should now flicker
change the local script to a server script and put it in server script service
is that the script or the module script
also does using this script make it so I can simply delete all the other scripts that are individually in each bulb
yes
also this will only apply to lights that are a direct descendant of WestBuildingLights
is there a way to replace text in a script bc I have two sets of lights ive been refrencing the hallway lighs
and the other lights use point instead of
or like diff light i mean
doesnt matter the script just does the flicker for any object that is a light period
doesnt matter if its surface point or spot
just call the function twice but change the WestBuildingLights to a refrence of the HallwayLights
(assuming hallway lights is also a model like west building lights)
both hallway lights and room lights are descendants of WestBuildingLights
** You are now Level 4! **
so would it run for every light inside the WestBuildingLights model?
then it will work fine
yes
alr alr
magic of :GetDescendants
also if you ever familiarize yourself with the code i sent you can use it for other stuff to
its completely modular
idk what that means but ill make sure to understand it as much as I can bc scripting that myself would take me so much
means it isnt really hard coded if you know what that means
it works for the hallways lights which use surfacelights but not the room lights which use pointlights
I think I understand, its flexible(?)
yes
you can use it for completely different functions and different types of functions
the room lights are a descendant of WestBuildingLights Correct?
yes
you mentioned something about changine Name to something earlier
assuming you meant somewhere here/?
Oh wait thats probs what it is
yeah
lemme update it for you
im gonna have you select every light in the game and name it the same name
the light itself?
wouldn't it be simpler to have it perform the script if it was called "PointLight" or "SurfaceLight"?
just question ima do what u say bc u know what ur doing
the for loop only flickers the child in this case if it has the name you provided in one of the arguments
ofc the studio doesnt let me mass rename when I select all the lights 🧍
and "pointlight" and "surfacelight" are different names
ahh ok ok
thats the one part thats its kind of limited in
is the if statement
but thats it
ok I have a lot of lights to rename so thats going to take a while, so I renamed them all to the same thing and in the parametes put the same name?
yes
so instead of "SurfaceLight" is just put whatever I call it
the first parameter of the function is the name you want the loop to search for
yeah
alr
you can ctrl click on all of them and select them all at once
is there any way to simutaniously change the name of multiple things
I do that but it wont let me rename them all
under properties
you can change the name there
also the name of the lights should be the same string you pass through the function
That would have been so useful like 3 hours ago when I was also naming the bulbs
LOL

Ok I think I got the whole script kinda understood, this helped a lot so thank you!
yeah no problem, is everything working how it should?
Yes, everythings working right now, I'm gonna continue working on the building now bc scripting is so confusing sometimes
alright man good luck with your game
thanks, hopefully I don't get burnt out and drop it bc i already put some effort in, thanks again!