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

How to add wait to function onTouch?

Asked by
Seyfert 90
7 years ago
Edited 7 years ago

I have this scirpt

local debounce = false

function onTouch(hit)
    wait(30)
    if not debounce then
    debounce = true


    hit.Anchored = false
    local explosion = Instance.new("Explosion", hit)
    explosion.DestroyJointRadiusPercent = 0
    explosion.Position = hit.Position


    return end
    debounce = false
    end

script.Parent.Touched:connect(onTouch)

I want to make it so even though the player touche's the bricks, there has to be a wait/reload of 30 seconds before the actual script will go. However, the wait(30) does not seem to be doing anything.

0
Line 15. Why are you using return? User#11440 120 — 7y
0
This is not your script try to learn scripting first! AuthenticOakChair 68 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Problem:

Your if statement checks if debounce is false which you've already set it to false so you want to check if it is true and then set it to true after-wards. Also, I recommend putting the wait(30) at the end just before putting your if statement to false. Anyways, the best way to explain my script is to show it to you and the commentate through it.

Here is my script(Looks a bit different)

function onTouch(hit) -- I set a function and named it 'onTouch' with a parameter of 'hit' just like you did.
    if debounce then return end -- Here is where it gets different I checked if it is true and if it is then it returns end. However, in this case our debounce would be false. I know I didn't set the debounce at the very top but, that's because it auto-matically thinks of it as false.
    debounce = true -- I set the debounce to true right here and think of this as if you're closing the gate to a function. So, when you set this to true this function can not be re-run unless you set the debounce back to false.
    hit.Anchored = false -- Your code
    local explosion = Instance.new("Explosion", hit) -- Your code
    explosion.DestroyJointRadiusPercent = 0 -- Your code
    explosion.Position = hit.Position -- Your code

wait(30) -- I moved the wait from the beginning of the function to the bottom so that it doesn't wait at the beginning. It fires it once and then waits for this time and then sets it to false. That is when it opens the gate to the function so that it can be re-run.
debounce = false -- It opens the gate to the function meaning that the function can run now since you set the debounce to false and the if statement would agree with the condition now. 
end -- This is an end for the function. If you've noticed I've already put an end to that if statement up there which is what it returns if debounce is true.

script.Parent.Touched:connect(onTouch) -- Connecting the function to an event. 

I hope i helped in one way or another if you have any questions please, post your questions in the comments below. I would love to help you some more.

~~ KingLoneCat

Ad
Log in to vote
1
Answered by 7 years ago

The part itself might have welds or joints, or some connection to other parts (this is my only possible explanation for the anchored issue). For the explosion, remember that explosion instances have a Position property that needs to be set to the part's position (http://wiki.roblox.com/index.php?title=API:Class/Explosion/Position).

Hope this has helped in any way :)

0
Yeah that helped, I got it btw its explosion.Position = hit.Position ....though the explosion kills me though I bet setting DestroyRadiusPercent to 0 would work Seyfert 90 — 7y

Answer this question