Hi ,
So, I want a part to drop only once when ever something touches it, but the part gets dropped 100 times. And I am using a Ontouched() function. Here is the code:
01 | function onTouched(hit) |
02 | local partDrop = script.Parent |
03 | local part = Instance.new( "Part" ,partDrop) |
04 | part.CFrame = partDrop.CFrame - Vector 3. new( 0 , 1.4 ,- 1 ) |
05 | part.FormFactor = "Custom" |
06 | part.Size = Vector 3. new( 0.75 , 0.75 , 0.75 ) |
07 | part.Material = "Neon" |
08 | part.TopSurface = "Smooth" |
09 | part.BottomSurface = "Smooth" |
10 | part.CanCollide = true |
11 | print ( "Dropped!" ) |
12 | game.Debris:AddItem(part,math.random( 2 , 5 )) |
13 | wait( 6 ) |
14 | end |
15 |
16 | script.Parent.Touched:Connect(onTouched) |
Thanks for Helping
Touched fires insanely, especially if the player stands on it. I recommend adding a debounce to most scripts that use an event.Here is the script with a debounce.
01 | local debounce = false --declaring |
02 | function onTouched(hit) |
03 | if debounce = = false then |
04 | debounce = true --making it true |
05 | local partDrop = script.Parent |
06 | local part = Instance.new( "Part" ,partDrop) |
07 | part.CFrame = partDrop.CFrame - Vector 3. new( 0 , 1.4 ,- 1 ) |
08 | part.FormFactor = "Custom" |
09 | part.Size = Vector 3. new( 0.75 , 0.75 , 0.75 ) |
10 | part.Material = "Neon" |
11 | part.TopSurface = "Smooth" |
12 | part.BottomSurface = "Smooth" |
13 | part.CanCollide = true |
14 | print ( "Dropped!" ) |
15 | game.Debris:AddItem(part,math.random( 2 , 5 )) |
01 | canSpawn = true |
02 | print ( "Ready!" ) |
03 |
04 | function onTouched(hit) |
05 | if canSpawn then |
06 | canSpawn = false |
07 | local partDrop = script.Parent |
08 | local part = Instance.new( "Part" ,partDrop) |
09 | part.CFrame = partDrop.CFrame - Vector 3. new( 0 , 1.4 ,- 1 ) |
10 | part.FormFactor = "Custom" |
11 | part.Size = Vector 3. new( 0.75 , 0.75 , 0.75 ) |
12 | part.Material = "Neon" |
13 | part.TopSurface = "Smooth" |
14 | part.BottomSurface = "Smooth" |
15 | part.CanCollide = true |
Hope this helped you