So this question links to my first one. I sort of figured out how to add a new model on button touch, but now, whenever I touch the block, the car keeps on being cloned, and crashes the game. Can you please help???
01 | local car = game.Workspace.car |
02 |
03 | function clone() |
04 | local yes = car:Clone() |
05 | yes.Parent = game.Workspace |
06 | yes.Position = Vector 3. new( 300.905 , 6.5 , - 275.073 ) |
07 |
08 | end |
09 |
10 | script.Parent.Touched:Connect(clone) |
Thank you!
a simple debounce should do the trick!
01 | local car = game.Workspace.car |
02 |
03 | local debounce = false |
04 |
05 | function clone() |
06 | if not debounce then |
07 | debounce = true |
08 | local yes = car:Clone() |
09 | yes.Parent = game.Workspace |
10 | yes.Position = Vector 3. new( 300.905 , 6.5 , - 275.073 ) |
11 |
12 | wait( 5 ) -- Set this to how long the debounce should be |
13 | debounce = false |
14 | end |
15 | end |
16 |
17 | script.Parent.Touched:Connect(clone) |
Edit:
01 | local car = game.Workspace.car |
02 |
03 | local debounce = false |
04 |
05 | function clone(hit) |
06 | local human = hit.Parent:FindFirstChild( "Humanoid" ) -- This basically just checks if the the thing touching it is a player, and it won't get pressed by any other object |
07 | if human then |
08 | if not debounce then |
09 | debounce = true |
10 | local yes = car:Clone() |
11 | yes.Parent = game.Workspace |
12 | yes.Position = Vector 3. new( 300.905 , 6.5 , - 275.073 ) |
13 |
14 | wait( 5 ) -- Set this to how long the debounce should be |
15 | debounce = false |
16 | end |
17 | end |
18 | end |
19 |
20 | script.Parent.Touched:Connect(clone) |
Hey you can add a cooldown by doing this:
01 | local car = game.Workspace.car |
02 | local cooldown = 3 -- put the cooldown in seconds here |
03 | local onCooldown = false |
04 |
05 | function clone() |
06 |
07 | if not onCooldown then |
08 | onCooldown = true |
09 | local yes = car:Clone() |
10 | yes.Parent = game.Workspace |
11 | yes.Position = Vector 3. new( 300.905 , 6.5 , - 275.073 ) |
12 |
13 | wait(cooldown) |
14 | onCooldown = false |
15 |
16 | end |
17 |
18 | end |
19 |
20 | script.Parent.Touched:Connect(clone) |
Haven't tested it but it should work
Alright, well I don't really know if this works, but combining your answers and some tutorials, I decided to go with the following. I don't know if this works...
01 | local car = game.Workspace.car |
02 | local cooldown = 3 -- put the cooldown in seconds here |
03 | local onCooldown = false |
04 |
05 | function clone(hit) |
06 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
07 | if not onCooldown then |
08 | onCooldown = true |
09 | local yes = car:Clone() |
10 | yes.Parent = game.Workspace |
11 | yes:MoveTo(Vector 3. new( 300.905 , 6.5 , - 275.073 )) |
12 |
13 | wait(cooldown) |
14 | onCooldown = false |
15 |
If I went wrong, please tell me where