#is there a way to have some sort of master script>?

226 messages · Page 1 of 1 (latest)

oblique junco
#

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?

silk hamletBOT
#

studio** You are now Level 1! **studio

tight nacelle
# oblique junco I have a lot of objects with the same script, so say I wanted to update that scr...

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)

oblique junco
#

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()?

tight nacelle
#

assuming the module is a direct ancestor of the objects yes

#

do you understand for loops?

oblique junco
#

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.

tight nacelle
#

also dont be calling the for loop every single second cuz it can get laggy if your spamming them

oblique junco
#

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

tight nacelle
oblique junco
#

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

oblique junco
tight nacelle
#

also for loops in lua are relitivly easy compared to alot of other launguages, you only really use them for tables and hierarchial stuff

oblique junco
#

but i didnt get pass learning aboutn _innit_

tight nacelle
#

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

oblique junco
#

I think so, im not sure but I think I remember seing that at some point and working with it

tight nacelle
#

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

oblique junco
#

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

oblique junco
tight nacelle
#

memorizing all the services and names and spelling takes a while

oblique junco
#

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

tight nacelle
#

Ui is really beginner friendly

#

thats what i really started off with

#

3 lines of code to make a really simple button work lmao

oblique junco
#

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

silk hamletBOT
#

studio** You are now Level 2! **studio

tight nacelle
oblique junco
#

how would I know the difference?

#

is just put like script and then named it and put some code in it

tight nacelle
#

is it a script or a local script

oblique junco
#

script

tight nacelle
#

local runs for the player only

#

script runs on the server for all players

oblique junco
#

oh

#

would it be less laggy to use local?

tight nacelle
#

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

oblique junco
#

I dont think so, it would'nt be fun to play on mobile since its kinda battle oriented

tight nacelle
#

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

oblique junco
#

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

tight nacelle
#

it honestly wouldnt and it would behave alot differently

oblique junco
#

how so?

tight nacelle
#

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

oblique junco
#

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?

tight nacelle
#

honestly if your going to implement it i would make it optional and on the client side

oblique junco
#

how would I go abt that, if you don't mind explaining, I have no idea how to do that

tight nacelle
#

make the for loop inside a client script within starter player scripts

oblique junco
#

If I have one script

#

oh

tight nacelle
oblique junco
tight nacelle
#

yes

#

you could just change the if statement to be something else

oblique junco
#

so something like

#

hm let me try it rq

tight nacelle
#

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

oblique junco
#

oh? I'll have to work on learning that soon it seems helpful

silk hamletBOT
#

studio** You are now Level 3! **studio

oblique junco
#

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
tight nacelle
oblique junco
#

would applying that script to workplace affect every bulb that is in every module named "Light"?

tight nacelle
#

module or model

oblique junco
#

model*

#

sorry they sound the same to me i get em confused

tight nacelle
#

i got confsued for a sec lol, here ill script it out for you

#

client sided

oblique junco
#

alr

oblique junco
#

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

tight nacelle
#

but if there unachored then that will cause extreme lag

oblique junco
#

yea im making sure to have them all anchored lmao

tight nacelle
#
-- 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

oblique junco
#

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

tight nacelle
#

put the search as WestBuildingLights

#

you would just have to refrence it in the script

#

put UseGetDescedant to true

oblique junco
#

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?

tight nacelle
oblique junco
#

oh right right

tight nacelle
#

bulb is however a descendant of westbuildinglights

#

put it in starterplayerscripts

#

local script

oblique junco
tight nacelle
#

actually wait im testing it an i made a error

#

lemme refactor

oblique junco
#

alr let me read this script over to get hopefully the understanding

#

most of the terms in it are new to me lmao

tight nacelle
#

oh wait i guess this cant be client sided

#

cuz its not able to refrence certain shit

#

mb

oblique junco
#

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

tight nacelle
oblique junco
#

hm

tight nacelle
#
-- 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

oblique junco
#

is that the script or the module script

tight nacelle
#

script

#

module scripts arnt client nor server

#

hard to explain

oblique junco
#

also does using this script make it so I can simply delete all the other scripts that are individually in each bulb

tight nacelle
#

yes

#

also this will only apply to lights that are a direct descendant of WestBuildingLights

oblique junco
#

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

tight nacelle
#

doesnt matter the script just does the flicker for any object that is a light period

#

doesnt matter if its surface point or spot

tight nacelle
#

(assuming hallway lights is also a model like west building lights)

oblique junco
#

both hallway lights and room lights are descendants of WestBuildingLights

silk hamletBOT
#

studio** You are now Level 4! **studio

oblique junco
#

so would it run for every light inside the WestBuildingLights model?

tight nacelle
#

yes

oblique junco
#

alr alr

tight nacelle
#

magic of :GetDescendants

oblique junco
#

i got an error

#

OH WAIT NVM

#

i forgot to define WestBuildingLights

#

mb

tight nacelle
#

also if you ever familiarize yourself with the code i sent you can use it for other stuff to

#

its completely modular

oblique junco
#

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

tight nacelle
#

means it isnt really hard coded if you know what that means

oblique junco
#

it works for the hallways lights which use surfacelights but not the room lights which use pointlights

oblique junco
tight nacelle
#

yes

#

you can use it for completely different functions and different types of functions

tight nacelle
oblique junco
#

yes

#

you mentioned something about changine Name to something earlier

#

assuming you meant somewhere here/?

tight nacelle
#

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

oblique junco
#

the light itself?

tight nacelle
#

yes

#

the thing that emits the light

#

name them all to "Light" or sum

oblique junco
#

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

tight nacelle
oblique junco
#

ofc the studio doesnt let me mass rename when I select all the lights 🧍

tight nacelle
#

and "pointlight" and "surfacelight" are different names

oblique junco
#

ahh ok ok

tight nacelle
#

thats the one part thats its kind of limited in

#

is the if statement

#

but thats it

oblique junco
#

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?

tight nacelle
#

yes

oblique junco
#

so instead of "SurfaceLight" is just put whatever I call it

tight nacelle
#

the first parameter of the function is the name you want the loop to search for

#

yeah

oblique junco
#

alr

tight nacelle
#

you can ctrl click on all of them and select them all at once

oblique junco
#

is there any way to simutaniously change the name of multiple things

#

I do that but it wont let me rename them all

tight nacelle
#

under properties

#

you can change the name there

#

also the name of the lights should be the same string you pass through the function

oblique junco
#

That would have been so useful like 3 hours ago when I was also naming the bulbs

tight nacelle
#

LOL

oblique junco
#

Ok I think I got the whole script kinda understood, this helped a lot so thank you!

tight nacelle
#

yeah no problem, is everything working how it should?

oblique junco
#

Yes, everythings working right now, I'm gonna continue working on the building now bc scripting is so confusing sometimes

tight nacelle
oblique junco
#

thanks, hopefully I don't get burnt out and drop it bc i already put some effort in, thanks again!