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.
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.
script:ShortenScript();
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:
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!
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
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.
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 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.
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.
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 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.
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!
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
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.
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?