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

How do I make this script more efficient and smaller?

Asked by 6 years ago
Frame1 = script.parent["Frame 1"]
Frame2 = script.parent["Frame 2"]

for i = 1, math.huge do -- 
   Frame1.Transparency = 1
   Frame1.CanCollide = false
   wait(0.2)
   Frame1.Transparency = 0
   Frame1.CanCollide = false
   Frame2.Transparency = 1
   Frame2.CanCollide = false
   wait(0.2)
   Frame2.Transparency = 0
   Frame2.CanCollide = true
end

The script does work fine, but if I were to add on more frames then it would just get way bigger, and I'm pretty sure having more than one of this script might cause lag too.

0
if you are using math.huge why don't you just use a while true do loop? User#21908 42 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Hi Birb,

To make a script more efficient, you need to make sure you don't repeat yourself. Also, try to minimize the amount of time you run the loop for. So, try to hesitate using infinite loops and such like the one you're using. I would recommend just running through all of the frames and then manipulating their properties.

I'd recommend storing all of the frames in a folder or something so that they're all in one place and you can just make a table by getting the children of the folder, rather than searching for each one.

If I were to make your script smaller and more efficient, it would look something like this:

local frames = script.Parent:WaitForChild("Frames Folder"):GetChildren();

local function change_props(obj, initial_props, end_props, t) -- obj is the frame and initial_props and end_props are dictionaries that have their indexes as property names and their values as the values for each property. Initial props are the first set of properties that the parts will become, and ending properties are the second set of properties that the parts will become.
    for name, prop in next, initial_props do -- for each iteration that has an index and a value
        obj[name] = prop; -- Gets the property and sets it to the value.
    end

    wait(t)

    for name, prop in next, end_props do -- for each iteration that has an index and a value
        obj[name] = prop; -- Gets the property and sets it to the value.
    end
end

for i = 1, #frames do -- Loops through all the frames
    local object = frames[i]; -- The frame

    local initialize_props = { -- What the properties will be first
        ["Transparency"] = 1;
        ["CanCollide"] = false;
    }
    local end_props = {
        ["Transparency"] = 0; -- What they will become after 0.2 seconds.
        ["CanCollide"] = true;      
    }
    local t = 0.2; -- The time it waits before changing from initial properties to end properties.

    change_props(object, initialize_props, end_props, t); -- Calls on the function and passes the object and the initial and ending properties, as well as the time it waits before changing the initial and ending properties
end

Hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

Ad

Answer this question