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

How do I shorten this script so I can reduce lag? [closed]

Asked by 6 years ago

I'm trying to make this script shorter to reduce lag (not much lag is added but it's still noticeable) but since I'm not super good at scripting I don't know of anyway.

01local enabled = false
02local cd = script.Parent.ClickDetector
03 
04function enabled()
05enabled = true
06script.Parent.Parent.GYSPA.PACERONE:Stop()
07script.Parent.Parent.GYSPB.PACERONE:Stop()
08script.Parent.Parent.GYSPC.PACERONE:Stop()
09script.Parent.Parent.GYSPD.PACERONE:Stop()
10script.Parent.Parent.GYSPE.PACERONE:Stop()
11script.Parent.Parent.GYSPF.PACERONE:Stop()
12script.Parent.Parent.GYSPG.PACERONE:Stop()
13script.Parent.Parent.GYSPH.PACERONE:Stop()
14script.Parent.Parent.GYSPA.PACERTWO:Stop()
15script.Parent.Parent.GYSPB.PACERTWO:Stop()
View all 87 lines...

It would mean a lot to me if you could help, thanks.

0
Is it sounds or animations that it's stopping? Launderer 343 — 6y
0
Sounds CaptainAlien132 225 — 6y
7
U Simply Cant green271 635 — 6y
View all comments (15 more)
1
Ah man, I was going to say that. User#21908 42 — 6y
0
you got a lot of answers to choose from User#23365 30 — 6y
0
poor op, 7 answers xddd User#19524 175 — 6y
0
Eight now. User#21908 42 — 6y
0
nine now, lets hit double digits lol theking48989987 2147 — 6y
0
8 User#19524 175 — 6y
1
How did this question ever spark this much attention lol Vulkarin 581 — 6y
1
Prefect Scripting Question does not exi- zafiruatest 75 — 6y
0
10. We'll get 11 soon. EzraNehemiah_TF2 3552 — 6y
0
Why did y'all get creative with the answers? GamingOverlord756 48 — 6y
0
12 User#19524 175 — 6y
1
I went off to go to sleep and I come back to this lol, these answers are hilarous CaptainAlien132 225 — 6y
0
choose an answer User#19524 175 — 6y
0
Captain alien is famous, Choose an answer captain, be even more famous ;o greatneil80 2647 — 6y
0
Wait what even is a gysph greatneil80 2647 — 6y

Locked by User#24403 and TheeDeathCaster

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

11 answers

Log in to vote
4
Answered by 6 years ago
Edited 6 years ago

Use a loop, this is gonna be the buggiest code I ever wrote T_T

01local n = false
02local function enable()
03for _,  v in pairs(script.Parent.Parent:GetChildren()) do
04    n = true
05    v.PACERONE:Stop()
06    v.PACERTWO:Stop()
07    v.PACERTHREE:Stop()
08    v.PACERFOUR:Stop()
09end
10end
11 
12local function disable()
13for _,  v in pairs(script.Parent.Parent:GetChildren()) do
14    n = false
15    v.PACERONE:Play()
View all 26 lines...

I am hoping that this works... It is a small loop, I didn't test it because I don't have much time to test this. I don't know if it will work or not, this is yours buddy, good luck!

If it works, click the answer button, thanks :)

I set it up in a loop, made it wait the timelength of a random PACER type for disabled and thats basically all.

0
um "v" is the value, so your just playing 'v' multiple times User#23365 30 — 6y
0
also do 'v:Play()` and 'v:Stop()` User#23365 30 — 6y
2
isn't that what he wanted? greatneil80 2647 — 6y
1
playing all then waiting a while greatneil80 2647 — 6y
View all comments (10 more)
0
you should learn more about for loops User#23365 30 — 6y
1
I already know a ton about "for" loops greatneil80 2647 — 6y
0
'v' is the value that the table is currently literating through, so v.PACERFOUR:Play() is just your accessing PACERFOUR inside PACERFOUR User#23365 30 — 6y
0
so your just playing the sound multiple times, and it'll return nil as PACERFOUR isnt valid member of the value User#23365 30 — 6y
2
You are dumb expodo, I am accessing the gysph stuff... then finding the pacers inside it greatneil80 2647 — 6y
2
He wants it to play multiple times omg... greatneil80 2647 — 6y
0
o wait User#23365 30 — 6y
0
o i didnt see that User#23365 30 — 6y
0
this'll wait about 7 more times than he wants (it'll wait for A, B, C, etc individually) User#22604 1 — 6y
1
I am pretty sure thats what he wants. greatneil80 2647 — 6y
Ad
Log in to vote
9
Answered by 6 years ago
1script:ShortenScript();
0
LMAO User#19524 175 — 6y
0
Lol User#21908 42 — 6y
0
I don't recognize that method. Maybe it's somewhere in the wiki I've never seen before. EzraNehemiah_TF2 3552 — 6y
0
Perfect! TheeDeathCaster 2368 — 6y
View all comments (4 more)
1
Official cockerel stamp of approval  mattscy 3725 — 6y
0
Yeah it does! Sadly that function is being optimized and that is why it says: "We can't find the page you're looking for." User#21908 42 — 6y
0
HAHAHAHA green271 635 — 6y
Log in to vote
6
Answered by 6 years ago
Edited 6 years ago
1script.Source = script.Source:sub(1, 1)
3
the answer should actually answer the question directly, not be copy pasted off a wiki User#22604 1 — 6y
2
It's not though. And why the downvote? This is not meant to answer it directly; I didn't answer it directly, but to leave as an exercise. That's typically better. User#19524 175 — 6y
0
Link the so called "wiki" he copy pasted off of. green271 635 — 6y
1
At least explain how this applies to his circumstance. User#21908 42 — 6y
View all comments (13 more)
0
the entire decrementing part was 100% irrelevant to his situation and it looked copy pasted based on all the irrelevant information. what i'd do if you want to leave it openended is something like "there are many types of loops, but what you want is this and how this works is..." so that you don't waste his time with a bunch of random information for him to dig through User#22604 1 — 6y
0
You literally posted a link to the wiki in the comments. ThatPreston 354 — 6y
1
The decrementing part could simply be ignored; and if you downvoted me for that, then that's s bad reason. Plus, it's best OP knows about the features just in case OP wishes to use them in the future. User#19524 175 — 6y
2
You are also accusing me of plagiarism without any valid evidence. User#19524 175 — 6y
0
^ TheeDeathCaster 2368 — 6y
0
^^ green271 635 — 6y
0
^^^ Lugical 425 — 6y
0
I recommend getting straight to the point, Incapaz; `while` and `repeat`'s used here as filler (not neccessary). I know I said in the past I posted answers to be used a references, but they at least had to do with the question. In this case, as others have pointed out, he could use a generic for; why not structure an answer around that? TheeDeathCaster 2368 — 6y
0
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa User#19524 175 — 6y
0
Why didn't you just do for and GetChildren() lol Vulkarin 581 — 6y
0
are you kidding me User#19524 175 — 6y
0
"apparently leaving exercises is not allowed so here you go" wat TheeDeathCaster 2368 — 6y
0
This answer has almost as many comments as the question. User#21908 42 — 6y
Log in to vote
6
Answered by
lunatic5 409 Moderation Voter
6 years ago
Edited 6 years ago

How to shorten a script

1
That's a pretty compact answer. EzraNehemiah_TF2 3552 — 6y
0
lool Vulkarin 581 — 6y
0
that worked HappyTimIsHim 652 — 6y
Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

The easiest way that is apparent to me is to have one function that both enables and disables the sounds. I will write an example of what you could do and explain how every element works:

01local function startOrStopSounds(obj, on)
02--[[
03    obj would be script.Parent.Parent and on would be
04    whether you want to play or stop the sound
05--]]
06    for i,v in pairs(obj:GetChildren()) do
07    --[[
08        looping through the children
09        of script.Parent.Parent
10    --]]
11        local childArray = v:GetChildren()
12        --[[
13            this returns an array of the
14            objects that are direct children of v
15        --]]
View all 50 lines...

I assume that you can take it from here. I just wanted to provide you with an example of how to efficiently go about this issue of yours. I hope this helps and have a great day scripting!

1
Too many comments, makes it harder to read. Also those additional checks make it harder to understand and are redundant. User#19524 175 — 6y
0
Yes his code was messy but who downvoted him? User#19524 175 — 6y
0
Note that this is an example and not a comprehensive answer. The other answers did a good enough job covering the basics. User#21908 42 — 6y
0
I just wanted to provide some examples that are more efficient/contain less code than the other answers. User#21908 42 — 6y
0
This was not meant to be a great answer, but a practical example of what the other answers have covered. With explanations of course. User#21908 42 — 6y
Log in to vote
2
Answered by 6 years ago

You can use a for loop to achieve this. It would also be more efficient to use one function instead of two. Basically, we'll loop through all of the sounds and toggle them accordingly.

01local Enabled = false
02local CD = script.Parent.ClickDetector
03 
04function Toggle(On)
05    for _, Descendant in pairs(script.Parent.Parent:GetDescendants()) do
06        if Descendant:IsA("Sound") then
07            if On then
08                Descendant:Play()
09            else
10                Descendant:Stop()
11            end
12        end
13    end
14end
15 
View all 26 lines...
Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

This answer's to be used as a reference

Just to change it up a little bit. :>

To accomplish something like this, you'd use a generic for to loop through the objects. Although it might be a good idea to use GetChildren to loop through the objects (as others have pointed out), I think GetDescendants would favor better, as we wont have to use two separate for loops to get the sounds and play/stop them. Here's an example:

1local ModelWithSounds = ... -- '...' being the model with the sounds.
2 
3for _, v in next, ModelWithSounds:GetDescendants() do -- You can use `pairs` or `next`; I just use `next` because it's my preference.
4    if v:IsA('Sound') then -- Checks if said current object's a Sound.
5        v:Play() -- If so, it'll play it.
6    end
7end

VS

1local ModelWithSounds = ...
2 
3for _, v in next, ModelWithSounds:GetChildren() do
4    for __, z in next, v:GetChildren() do -- A second loop to loop through the current object.
5        if z:IsA('Sound') then -- Checks the object within another object's a sound.
6            z:Play() -- I think you get the point. XP
7        end
8    end
9end

Pretty short and sweet answer, right? :>

Now, onto...

Stuff touched on, but never really explained

  1. Generic For - To quote the page, "The generic for loop allows you to traverse all values returned by an iterator function." In other words, it allows you to iterate over a table; it'll return two values: The index, and the value for said index.

  2. GetChildren - Creates a table with all the children within a object.

  3. GetDescendants - Similar to GetChildren, but it also accounts for descendants; for example, if there's a model with parts in the Workspace, Workspace:GetDescendants() will get the model and the parts within the model.

  4. IsA -- To put simply, it checks an object's ClassName, and if the ClassName is what you gave IsA, it'll return true, otherwise it'll return false.

If you have any questions, please let me know. Thanks for reading, and have a good day. :)

0
was this really necessary? User#19524 175 — 6y
0
yeee TheeDeathCaster 2368 — 6y
0
Well, probably not. I just wanted to post my own answer on this topic, and add a *little* bit of spice into the mix. :> TheeDeathCaster 2368 — 6y
0
/offtopic/ Wow, so many answers now. o.o TheeDeathCaster 2368 — 6y
Log in to vote
2
Answered by
hellmatic 1523 Moderation Voter
6 years ago
Edited 6 years ago

Use for loops to edit each child individually. Make a variable for script.Parent.Parent instead of repeating script.Parent.Parent for multiple lines.

1local ParentModel = script.Parent.Parent
2 
3-- ParentModel:GetChildren() returns a table of ParentModel's children.
4-- To edit each children individually, you'll have to use a for loop to iterate through the table
5for i, v in pairs(ParentModel:GetChildren()) do -- think of 'v' as the child you are acquiring from the table. You can name 'v' anything you prefer.
6    -- code
7end
Log in to vote
2
Answered by 6 years ago

The bracket operator

The dot operator (.) is similar to the bracket operator in that we can use it to get children of an object and get properties like so:

1local part = workspace.Part --I used a dot
2local part2 = workspace["2Part"] --I used square brackets instead

The bonus of this is that we need to use a string to retrieve a child/property with that name. The good thing about using a string instead of just the object name is that we can use spaces, it can start with a number and we can concatenate strings.


Concatenation

When you concatenate something, you are combining it together. We concatenate strings by putting 2 periods in between the strings. Here is an example of concatenating 2 strings together:

1local color = "Blue"
2local color2 = "Pink"
3 
4print(color.." and "..color2.." are my favorite colors")

Blue and Pink are my favorite colors

It combines both strings into 1.


Simplifying your function

We can actually turn your function into a single script by combining your two functions into 1. We can use your variable enabled as a way to see if we should enable or disable your objects.

01local enabled = false
02local cd = script.Parent.ClickDetector
03 
04cd.MouseClicked:Connect(function()
05    if enabled then
06        --If enabled is true then...
07    else
08        --If disabled then...
09    end
10end)

For loops

For loop will loop over and over again for a set amount of times. We can use this to our advantage to make it loop for every item inside of a table. We can use pairs to return the objects inside of a table.

01for i = 1,10 do
02    print(i)
03end
04--prints 1-10
05 
06local array = {true, false, workspace}
07 
08for i, v in pairs(array) do --i stands for iteration or how many times it has looped
09    print(v) --v stands for value. These variables can be whatever you want.
10end

The first array loops 10 times because we set it to do that. The second one will loop 3 times because we said we wanted it to loop for each value inside of the array.


Final Product

01local enabled = false
02local cd = script.Parent.ClickDetector
03local letters = {"A", "B", "C", "D", "E", "F", "G", "H"} --These will be concatenated
04for i,v in pairs(letters) do
05    letters[i] = "GYSP"..v
06end
07local numbers = {"ONE", "TWO", "THREE", "FOUR"}
08for i,v in pairs(numbers) do
09    for i2,v2 in pairs(letters) do
10        numbers[i2] = script.Parent.Parent[v2]["PACER"..v]
11    end
12end
13 
14cd.MouseClicked:Connect(function()
15    enabled = not enabled --If false, change to true... and vise versa
View all 28 lines...


Hope it helps!

0
lol User#19524 175 — 6y
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

you can use a generic for loop which literates through a table, in which :GetChildren() returns.

1for i,v in pairs(script.Parent.Parent:GetChildren()) do
2    if string.match(v.Name,"GYSP") then
3        local sound = v:FindFirstChildOfClass("Sound")
4 
5        if sound then
6            sound:Play()
7        end
8    end
9end
0
thing is he has gyspa-gysph User#22604 1 — 6y
0
wat User#23365 30 — 6y
1
I have multiple speakers (gymspeakera shortened to gyspa to gysph) so this would only play from a single speaker CaptainAlien132 225 — 6y
0
edited, i didnt see that User#23365 30 — 6y
View all comments (4 more)
0
doesn't do the waiting he wants, doesn't do the enable/disable he wants, doesn't care about it being clicked... not really what he was asking for User#22604 1 — 6y
0
also he has three sounds in each GYSP object it looks like User#22604 1 — 6y
1
im just gonna edit this tomorrow, its almost 10pm and im doing my homework User#23365 30 — 6y
0
this is a general idea of how he could shorten his code User#23365 30 — 6y
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago
1script.Parent.Parent:Destroy()

pretty simple script that will help 100% if it doesn't work then tell me because i wouldn't know why and if it did something you didn't want it to do then try asking your questions more specifically.

0
i luv it User#23365 30 — 6y
0
^ User#21908 42 — 6y