Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I shorten this script so I can reduce lag?

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.

local enabled = false
local cd = script.Parent.ClickDetector

function enabled()
enabled = true
script.Parent.Parent.GYSPA.PACERONE:Stop()
script.Parent.Parent.GYSPB.PACERONE:Stop()
script.Parent.Parent.GYSPC.PACERONE:Stop()
script.Parent.Parent.GYSPD.PACERONE:Stop()
script.Parent.Parent.GYSPE.PACERONE:Stop()
script.Parent.Parent.GYSPF.PACERONE:Stop()
script.Parent.Parent.GYSPG.PACERONE:Stop()
script.Parent.Parent.GYSPH.PACERONE:Stop()
script.Parent.Parent.GYSPA.PACERTWO:Stop()
script.Parent.Parent.GYSPB.PACERTWO:Stop()
script.Parent.Parent.GYSPC.PACERTWO:Stop()
script.Parent.Parent.GYSPD.PACERTWO:Stop()
script.Parent.Parent.GYSPE.PACERTWO:Stop()
script.Parent.Parent.GYSPF.PACERTWO:Stop()
script.Parent.Parent.GYSPG.PACERTWO:Stop()
script.Parent.Parent.GYSPH.PACERTWO:Stop()
script.Parent.Parent.GYSPA.PACERTHREE:Stop()
script.Parent.Parent.GYSPB.PACERTHREE:Stop()
script.Parent.Parent.GYSPC.PACERTHREE:Stop()
script.Parent.Parent.GYSPD.PACERTHREE:Stop()
script.Parent.Parent.GYSPE.PACERTHREE:Stop()
script.Parent.Parent.GYSPF.PACERTHREE:Stop()
script.Parent.Parent.GYSPG.PACERTHREE:Stop()
script.Parent.Parent.GYSPH.PACERTHREE:Stop()
script.Parent.Parent.GYSPA.PACERFOUR:Stop()
script.Parent.Parent.GYSPB.PACERFOUR:Stop()
script.Parent.Parent.GYSPC.PACERFOUR:Stop()
script.Parent.Parent.GYSPD.PACERFOUR:Stop()
script.Parent.Parent.GYSPE.PACERFOUR:Stop()
script.Parent.Parent.GYSPF.PACERFOUR:Stop()
script.Parent.Parent.GYSPG.PACERFOUR:Stop()
script.Parent.Parent.GYSPH.PACERFOUR:Stop()
end

function disabled()
enabled = false
script.Parent.Parent.GYSPA.PACERONE:Play()
script.Parent.Parent.GYSPB.PACERONE:Play()
script.Parent.Parent.GYSPC.PACERONE:Play()
script.Parent.Parent.GYSPD.PACERONE:Play()
script.Parent.Parent.GYSPE.PACERONE:Play()
script.Parent.Parent.GYSPF.PACERONE:Play()
script.Parent.Parent.GYSPG.PACERONE:Play()
script.Parent.Parent.GYSPH.PACERONE:Play()
wait(script.Parent.Parent.GYSPA.PACERONE.TimeLength)
script.Parent.Parent.GYSPA.PACERTWO:Play()
script.Parent.Parent.GYSPB.PACERTWO:Play()
script.Parent.Parent.GYSPC.PACERTWO:Play()
script.Parent.Parent.GYSPD.PACERTWO:Play()
script.Parent.Parent.GYSPE.PACERTWO:Play()
script.Parent.Parent.GYSPF.PACERTWO:Play()
script.Parent.Parent.GYSPG.PACERTWO:Play()
script.Parent.Parent.GYSPH.PACERTWO:Play()
wait(script.Parent.Parent.GYSPA.PACERTWO.TimeLength)
script.Parent.Parent.GYSPA.PACERTHREE:Play()
script.Parent.Parent.GYSPB.PACERTHREE:Play()
script.Parent.Parent.GYSPC.PACERTHREE:Play()
script.Parent.Parent.GYSPD.PACERTHREE:Play()
script.Parent.Parent.GYSPE.PACERTHREE:Play()
script.Parent.Parent.GYSPF.PACERTHREE:Play()
script.Parent.Parent.GYSPG.PACERTHREE:Play()
script.Parent.Parent.GYSPH.PACERTHREE:Play()
wait(script.Parent.Parent.GYSPA.PACERTHREE.TimeLength)
script.Parent.Parent.GYSPA.PACERFOUR:Play()
script.Parent.Parent.GYSPB.PACERFOUR:Play()
script.Parent.Parent.GYSPC.PACERFOUR:Play()
script.Parent.Parent.GYSPD.PACERFOUR:Play()
script.Parent.Parent.GYSPE.PACERFOUR:Play()
script.Parent.Parent.GYSPF.PACERFOUR:Play()
script.Parent.Parent.GYSPG.PACERFOUR:Play()
script.Parent.Parent.GYSPH.PACERFOUR:Play()
end

function onClicked()
	
if enabled == true then disabled() else enabled()  end
		
end
	
cd.MouseClick:connect(onClicked)
		
enabled()

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

19 comments

0

Is it sounds or animations that it's stopping?

0

Sounds

7

U Simply Cant

1

Ah man, I was going to say that.

0

you got a lot of answers to choose from

0

poor op, 7 answers xddd

0

Eight now.

0

nine now, lets hit double digits lol

0

8

1

How did this question ever spark this much attention lol

1

Prefect Scripting Question does not exi-

0
  1. We'll get 11 soon.
0

Why did y'all get creative with the answers?

0

12

1

I went off to go to sleep and I come back to this lol, these answers are hilarous

0

choose an answer

0

Captain alien is famous, Choose an answer captain, be even more famous ;o

0

Wait what even is a gysph

11 answers

✓ Accepted answer

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

local n = false
local function enable()
for _,  v in pairs(script.Parent.Parent:GetChildren()) do
	n = true
	v.PACERONE:Stop()
	v.PACERTWO:Stop()
	v.PACERTHREE:Stop()
	v.PACERFOUR:Stop()
end
end

local function disable()
for _,  v in pairs(script.Parent.Parent:GetChildren()) do
	n = false
	v.PACERONE:Play()
	wait(v.PACERONE.TimeLength)
	v.PACERTWO:Play()
	wait(v.PACERTWO.TimeLength)
	v.PACERTHREE:Play()
	wait(v.PACERTHREE.TimeLength)
	v.PACERFOUR:Play()
end
end
script.Parent.ClickDetector.MouseClick:Connect(function()
	if n == true then disable() else enable() end
end)

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.

14 comments

0

um "v" is the value, so your just playing 'v' multiple times

0

also do 'v:Play() and 'v:Stop()

2

isn't that what he wanted?

1

playing all then waiting a while

0

you should learn more about for loops

1

I already know a ton about "for" loops

0

'v' is the value that the table is currently literating through, so v.PACERFOUR:Play() is just your accessing PACERFOUR inside PACERFOUR

0

so your just playing the sound multiple times, and it'll return nil as PACERFOUR isnt valid member of the value

2

You are dumb expodo, I am accessing the gysph stuff... then finding the pacers inside it

2

He wants it to play multiple times omg...

0

o wait

0

o i didnt see that

0

this'll wait about 7 more times than he wants (it'll wait for A, B, C, etc individually)

1

I am pretty sure thats what he wants.

script:ShortenScript();

8 comments

0

LMAO

0

Lol

0

I don't recognize that method. Maybe it's somewhere in the wiki I've never seen before.

0

Perfect!

1

Official cockerel stamp of approval 

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."

0

HAHAHAHA

script.Source = script.Source:sub(1, 1)

17 comments

3

the answer should actually answer the question directly, not be copy pasted off a wiki

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.

0

Link the so called "wiki" he copy pasted off of.

1

At least explain how this applies to his circumstance.

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

0

You literally posted a link to the wiki in the comments.

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.

2

You are also accusing me of plagiarism without any valid evidence.

0

^

0

^^

0

^^^

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?

0

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

0

Why didn't you just do for and GetChildren() lol

0

are you kidding me

0

"apparently leaving exercises is not allowed so here you go" wat

0

This answer has almost as many comments as the question.

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:

local function startOrStopSounds(obj, on) 
--[[ 
	obj would be script.Parent.Parent and on would be 
	whether you want to play or stop the sound
--]]
	for i,v in pairs(obj:GetChildren()) do 
	--[[
		looping through the children
		of script.Parent.Parent
	--]]
		local childArray = v:GetChildren() 
		--[[ 
			this returns an array of the 
			objects that are direct children of v
		--]]
		if  #childArray > 0 then
		--[[
			here is where we check if the array that 
			was returned by :GetChildren() has more 
			than zero elements
		--]]
			for i,v in pairs(childArray) do 
			--[[
				 now we know it has 
				children so we will loop
			--]]
				if v:IsA("Sound") then 
			--[[
				if the child is a sound then we will 
				either play or stop based on whether or not the 
				argument passed to the parameter on is true or false
			--]]
					if on then -- if on is true  then
						v:Play() -- turning on the sound
					else -- if on is false then
						v:Stop() -- turning off the sound
					end
				end
			end
		end
	end
end
-- here is an example of calling the function:
startOrStopSounds(script.Parent.Parent, true)
--[[
	I do recommend that you use variables, 
	namely for script.Parent.Parent
	it can reduce your typing amount and 
	cause less lag even if it is not much
--]]

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!

5 comments

1

Too many comments, makes it harder to read. Also those additional checks make it harder to understand and are redundant.

0

Yes his code was messy but who downvoted him?

0

Note that this is an example and not a comprehensive answer. The other answers did a good enough job covering the basics.

0

I just wanted to provide some examples that are more efficient/contain less code than the other answers.

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.

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.

local Enabled = false
local CD = script.Parent.ClickDetector

function Toggle(On)
	for _, Descendant in pairs(script.Parent.Parent:GetDescendants()) do
		if Descendant:IsA("Sound") then
			if On then
				Descendant:Play()
			else
				Descendant:Stop()
			end
		end
	end
end

CD.MouseClick:Connect(function(Player)
	if Enabled then
		Enabled = false
		Toggle(false)
	else
		Enabled = true
		Toggle(true)
	end
end)

Toggle(true)

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:

local ModelWithSounds = ... -- '...' being the model with the sounds.

for _, v in next, ModelWithSounds:GetDescendants() do -- You can use `pairs` or `next`; I just use `next` because it's my preference.
	if v:IsA('Sound') then -- Checks if said current object's a Sound.
		v:Play() -- If so, it'll play it.
	end
end

VS

local ModelWithSounds = ...

for _, v in next, ModelWithSounds:GetChildren() do
	for __, z in next, v:GetChildren() do -- A second loop to loop through the current object.
		if z:IsA('Sound') then -- Checks the object within another object's a sound.
			z:Play() -- I think you get the point. XP
		end
	end
end

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. :)

4 comments

0

was this really necessary?

0

yeee

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. :>

0

/offtopic/ Wow, so many answers now. o.o

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

local ParentModel = script.Parent.Parent

-- ParentModel:GetChildren() returns a table of ParentModel's children.
-- To edit each children individually, you'll have to use a for loop to iterate through the table
for 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.
	-- code
end

#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:

local part = workspace.Part --I used a dot
local 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:

local color = "Blue"
local color2 = "Pink"

print(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.

local enabled = false
local cd = script.Parent.ClickDetector

cd.MouseClicked:Connect(function()
	if enabled then
		--If enabled is true then...
	else
		--If disabled then...
	end
end)

#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.

for i = 1,10 do
	print(i)
end
--prints 1-10

local array = {true, false, workspace}

for i, v in pairs(array) do --i stands for iteration or how many times it has looped
	print(v) --v stands for value. These variables can be whatever you want.
end

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

local enabled = false
local cd = script.Parent.ClickDetector
local letters = {"A", "B", "C", "D", "E", "F", "G", "H"} --These will be concatenated
for i,v in pairs(letters) do
	letters[i] = "GYSP"..v
end
local numbers = {"ONE", "TWO", "THREE", "FOUR"}
for i,v in pairs(numbers) do
	for i2,v2 in pairs(letters) do
		numbers[i2] = script.Parent.Parent[v2]["PACER"..v]
	end
end

cd.MouseClicked:Connect(function()
	enabled = not enabled --If false, change to true... and vise versa
	if enabled then --If it's enabled, then disable
		for _,v in pairs(numbers) do
			v:Stop()
		end
	else
		for i = 0,3 do
			for i2 = (i*4+1),(i*4+4) do
				numbers[i2]:Play()
			end
			wait(numbers[i*4+1].TimeLength)
		end
	end
end)


Hope it helps!

1 comment

0

lol

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

for i,v in pairs(script.Parent.Parent:GetChildren()) do
	if string.match(v.Name,"GYSP") then
		local sound = v:FindFirstChildOfClass("Sound")
		
		if sound then
			sound:Play()
		end
	end
end

8 comments

0

thing is he has gyspa-gysph

0

wat

1

I have multiple speakers (gymspeakera shortened to gyspa to gysph) so this would only play from a single speaker

0

edited, i didnt see that

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

0

also he has three sounds in each GYSP object it looks like

1

im just gonna edit this tomorrow, its almost 10pm and im doing my homework

0

this is a general idea of how he could shorten his code

script.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.

2 comments

0

i luv it

0

^

This discussion is preserved as it was. New answers, comments, and votes are closed.