bloberous
|
  |
| Joined: 07 Jun 2009 |
| Total Posts: 299 |
|
|
| 14 Jul 2016 11:37 PM |
I wanted to make a brick move 30 times by one stud every time on the x-axis whenever it is touched. It works fine, but if you touch it repeatedly, it'll jitter around meaning (I think) that the function is stacked multiple times. How do I make it so that when it is touched, the function cannot be called until it is finished?
function move(hit) if hit.Parent:findFirstChild'Humanoid' ~= nil then local x = script.Parent.Position.X for i = 1, 30, 1 do script.Parent.Position = Vector3.new(x+i, script.Parent.Position.Y, script.Parent.Position.Z) wait(1) end end end
script.Parent.Touched:connect(move) |
|
|
| Report Abuse |
|
|
lululukas
|
  |
| Joined: 23 Aug 2010 |
| Total Posts: 1043 |
|
|
| 14 Jul 2016 11:40 PM |
The thing is, it's finished in a fraction of a second. So if you touch the brick, the touch event will be getting triggered like 10 times. We use a debounce for this
db=false script.Parent.Touched:connect(function(hit) if db==false then db=true if hit.Parent:findFirstChild'Humanoid' ~= nil then local x = script.Parent.Position.X for i = 1, 30, 1 do script.Parent.Position = Vector3.new(x+i, script.Parent.Position.Y, script.Parent.Position.Z) wait(1) end end wait(1) db=false end)
|
|
|
| Report Abuse |
|
|
|
| 14 Jul 2016 11:41 PM |
You create a Boolean. The first thing is put local Toggle = false, and the top of the script, not the function.
Before the if, put Toggle = true
In the for loop, put if Toggle == false then
After the script.Part string, put else return end
After the wait, put Toggle = false |
|
|
| Report Abuse |
|
|
bloberous
|
  |
| Joined: 07 Jun 2009 |
| Total Posts: 299 |
|
|
| 14 Jul 2016 11:49 PM |
| That makes so much sense! Thanks heaps from the both of you! |
|
|
| Report Abuse |
|
|
bloberous
|
  |
| Joined: 07 Jun 2009 |
| Total Posts: 299 |
|
|
| 14 Jul 2016 11:58 PM |
| Actually I have another problem with this. When the character touches it as it is moving, it goes above the character as to not interfere with the character's position. Any way to prevent that too? |
|
|
| Report Abuse |
|
|
|
| 15 Jul 2016 12:07 AM |
| I think setting it's CFrame instead of it's position will do the trick. |
|
|
| Report Abuse |
|
|
lululukas
|
  |
| Joined: 23 Aug 2010 |
| Total Posts: 1043 |
|
| |
|
bloberous
|
  |
| Joined: 07 Jun 2009 |
| Total Posts: 299 |
|
|
| 15 Jul 2016 12:14 AM |
| My God it is beautiful. It feels so good to make a script from scratch and have it work the way you want it to. Thanks for everyone's help! |
|
|
| Report Abuse |
|
|