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.
01 | local enabled = false |
02 | local cd = script.Parent.ClickDetector |
03 |
04 | function enabled() |
05 | enabled = true |
06 | script.Parent.Parent.GYSPA.PACERONE:Stop() |
07 | script.Parent.Parent.GYSPB.PACERONE:Stop() |
08 | script.Parent.Parent.GYSPC.PACERONE:Stop() |
09 | script.Parent.Parent.GYSPD.PACERONE:Stop() |
10 | script.Parent.Parent.GYSPE.PACERONE:Stop() |
11 | script.Parent.Parent.GYSPF.PACERONE:Stop() |
12 | script.Parent.Parent.GYSPG.PACERONE:Stop() |
13 | script.Parent.Parent.GYSPH.PACERONE:Stop() |
14 | script.Parent.Parent.GYSPA.PACERTWO:Stop() |
15 | script.Parent.Parent.GYSPB.PACERTWO:Stop() |
It would mean a lot to me if you could help, thanks.
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?
Use a loop, this is gonna be the buggiest code I ever wrote T_T
01 | local n = false |
02 | local function enable() |
03 | for _, 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() |
09 | end |
10 | end |
11 |
12 | local function disable() |
13 | for _, v in pairs (script.Parent.Parent:GetChildren()) do |
14 | n = false |
15 | v.PACERONE:Play() |
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.
1 | script:ShortenScript(); |
1 | script.Source = script.Source:sub( 1 , 1 ) |
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:
01 | local 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 | --]] |
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!
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.
01 | local Enabled = false |
02 | local CD = script.Parent.ClickDetector |
03 |
04 | function 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 |
14 | end |
15 |
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:
1 | local ModelWithSounds = ... -- '...' being the model with the sounds. |
2 |
3 | for _, 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 |
7 | end |
VS
1 | local ModelWithSounds = ... |
2 |
3 | for _, 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 |
9 | end |
Pretty short and sweet answer, right? :>
Now, onto...
Stuff touched on, but never really explained
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.
GetChildren
- Creates a table with all the children within a object.
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.
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. :)
Use for loops to edit each child individually. Make a variable for script.Parent.Parent
instead of repeating script.Parent.Parent for multiple lines.
1 | local 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 |
5 | 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. |
6 | -- code |
7 | end |
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:
1 | local part = workspace.Part --I used a dot |
2 | local part 2 = 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.
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:
1 | local color = "Blue" |
2 | local color 2 = "Pink" |
3 |
4 | print (color.. " and " ..color 2.. " are my favorite colors" ) |
Blue and Pink are my favorite colors
It combines both strings into 1.
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.
01 | local enabled = false |
02 | local cd = script.Parent.ClickDetector |
03 |
04 | cd.MouseClicked:Connect( function () |
05 | if enabled then |
06 | --If enabled is true then... |
07 | else |
08 | --If disabled then... |
09 | end |
10 | end ) |
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.
01 | for i = 1 , 10 do |
02 | print (i) |
03 | end |
04 | --prints 1-10 |
05 |
06 | local array = { true , false , workspace } |
07 |
08 | for 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. |
10 | 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.
01 | local enabled = false |
02 | local cd = script.Parent.ClickDetector |
03 | local letters = { "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" } --These will be concatenated |
04 | for i,v in pairs (letters) do |
05 | letters [ i ] = "GYSP" ..v |
06 | end |
07 | local numbers = { "ONE" , "TWO" , "THREE" , "FOUR" } |
08 | for i,v in pairs (numbers) do |
09 | for i 2 ,v 2 in pairs (letters) do |
10 | numbers [ i 2 ] = script.Parent.Parent [ v 2 ] [ "PACER" ..v ] |
11 | end |
12 | end |
13 |
14 | cd.MouseClicked:Connect( function () |
15 | enabled = not enabled --If false, change to true... and vise versa |
Hope it helps!
you can use a generic for loop
which literates through a table, in which :GetChildren()
returns.
1 | for 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 |
9 | end |
1 | 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.