This script is made to where you hit a brick, a figure and sound plays. I'm having trouble on how to script intervals of wait times. So when you hit the brick it plays sound and shows the figure for a second or so, then make a 30 second period where the script wont activate when hitting the brick. Here's the script. Your help would make my day a lot better lol
Switcher = script.Parent Figure = script.Parent.Parent.theFigure
function onTouched(Switcher) local boolean = true if boolean == true then Figure.Head.Transparency = .7 Figure.Head.CanCollide = false Figure.Torso.Transparency = .7 Figure.Torso.CanCollide = false Figure.Part1.Transparency = .7 Figure.Part1.CanCollide = false Figure.Part2.Transparency = .7 Figure.Part2.CanCollide = false Figure.Part3.Transparency = .7 Figure.Part3.CanCollide = false Figure.Part4.Transparency = .7 Figure.Part4.CanCollide = false script.Parent.Sound:Play()
boolean = false wait(.5) Figure.Head.Transparency = 1 Figure.Head.CanCollide = false Figure.Torso.Transparency = 1 Figure.Torso.CanCollide = false Figure.Part1.Transparency = 1 Figure.Part1.CanCollide = false Figure.Part2.Transparency = 1 Figure.Part2.CanCollide = false Figure.Part3.Transparency = 1 Figure.Part3.CanCollide = false Figure.Part4.Transparency = 1 Figure.Part4.CanCollide = false script.Parent.Sound:Play() boolean = true end end
script.Parent.Touched:connect(onTouched)
Switcher = script.Parent Figure = script.Parent.Parent.theFigure function onTouched(Switcher) local boolean = true if boolean == true then Figure.Head.Transparency = .7 Figure.Head.CanCollide = false Figure.Torso.Transparency = .7 Figure.Torso.CanCollide = false Figure.Part1.Transparency = .7 Figure.Part1.CanCollide = false Figure.Part2.Transparency = .7 Figure.Part2.CanCollide = false Figure.Part3.Transparency = .7 Figure.Part3.CanCollide = false Figure.Part4.Transparency = .7 Figure.Part4.CanCollide = false script.Parent.Sound:Play() boolean = false wait(.5) Figure.Head.Transparency = 1 Figure.Head.CanCollide = false Figure.Torso.Transparency = 1 Figure.Torso.CanCollide = false Figure.Part1.Transparency = 1 Figure.Part1.CanCollide = false Figure.Part2.Transparency = 1 Figure.Part2.CanCollide = false Figure.Part3.Transparency = 1 Figure.Part3.CanCollide = false Figure.Part4.Transparency = 1 Figure.Part4.CanCollide = false script.Parent.Sound:Play() boolean = true end end script.Parent.Touched:connect(onTouched)
There are a lot of problems, and a lot of mixed up code;
Line 1
, What is the point of it? It isn't being used anywhere in the code!
Line 2
, When it comes to that, we must question, What if the instance is not existant at time of execution of the code?
Because, as that code currently stands, it will error if the instance theFigure
is not existant at the time of execution, and will break your code, consider using the WaitForChild method?
Line 4
, You have your Debounce inside the function! The problem with that is; When someone touches the BasePart type instance, it will create a new local Variable, thus, defeating the point of that debounce!
Lines 5-16
, and 21-32
, That much code is NOT necessary! Consider using the For loop, and the GetChildren method?
Now, as I have stated, there are allot of problems in your code, which is causing it not to work properly, however, this can all be fixed by applying what I have pointed out. Now, let's fix up the code;
local Figure = script.Parent.Parent:WaitForChild("theFigure") --The 'WaitForChild' method will repeat waiting for the Child until it is existant, then will return it local boolean = true; --Here is your 'Debounce', outside of the function; The debounce is used to keep an event/function from executing multiple times at once function onTouched() --Here is your original function; I took out the argument as it is not used anywhere if not boolean then return false end --This will check to see if variable 'boolean' is 'false', and if so, it will return, and not let the code go through boolean = false --Set's 'boolean' to 'false' for i,v in pairs(Figure:GetChildren()) do --Here is our 'for' loop now; The for loop will loop through all the current Children within 'Figure'; The 'GetChildren' method will create a table of the current Children within 'Figure' [Parent:GetChildren()] coroutine.wrap(function() --We are using a coroutine to prevent the chunk from waiting multiple times for each Child if v:IsA("BasePart") then --This will check to see if the current Child the loop is looping through is a 'BasePart' type instance [WedgePart, Part, CornerWedgePart, ect.] v.Transparency = .7 --Will revert the Child's Transparency to 0.7 v.CanCollide = false --Will revert the Child's CanCollide [Allow things to walk through or not] to false, allow instances to go through it wait(.5) --Waits '.5' seconds v.Transaprency = 1 --Will revert the Child's transparency to 1? v.CanCollide = true --Will revert the Child's CanCollide to true, not allow things to go through end --This ends the chunk for the 'if' statement end)() --This ends the chunk for the coroutine end --This ends the chunk for the 'for' loop wait(2) --Waits 2 seconds boolean = true --Reverts 'boolean' to true, allow the user to fun the chunk of code again end --Ends the chunk for the function script.Parent.Touched:connect(onTouched) --Whenever the BasePart type instance is touched, the event '.Touched' will fire the function 'onTouched'
Ah, now doesn't your code look a lot cleaner? ;)
A few things I may not have explained in-depth;
The WaitForChild method will repeat waiting until a Child with a matching name of the Argument is existant, then will return the Child.
The GetChildren method creates a table of the current children within the Parent [Parent:GetChildren()], then will return the table.
The if satement will check the Variable if it is true or not, or, if the Variable is equal to the Argument [if Variable == Argument then Run code end code], but, however, if the Variable or not equal to the Argument, or, if it is not true, it will not fire the code unless you use the Else keyword.
The for loop has multiple purposes, in this case, it's purpose is used to loop through the current table of the Children, from the GetChildren
method.
A Debounce is used to prevent a chunk of code, or, a function, from firing multiple times at once on execution.
Sorry if my Answer is not very in-depth, or sound a bit harsh, I forgot how I originally wrote my Answers.
Hope this helped!