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

How to make only one part to drop instead of hundreds?

Asked by 5 years ago

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:

01function onTouched(hit)
02    local partDrop = script.Parent
03    local part = Instance.new("Part",partDrop)
04    part.CFrame = partDrop.CFrame - Vector3.new(0,1.4,-1)
05    part.FormFactor = "Custom"
06    part.Size=Vector3.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)
14end
15 
16script.Parent.Touched:Connect(onTouched)

Thanks for Helping

2 answers

Log in to vote
0
Answered by 5 years ago

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.

01local debounce=false--declaring
02function onTouched(hit)
03if  debounce==false then
04debounce=true--making it true
05    local partDrop = script.Parent
06    local part = Instance.new("Part",partDrop)
07    part.CFrame = partDrop.CFrame - Vector3.new(0,1.4,-1)
08    part.FormFactor = "Custom"
09    part.Size=Vector3.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))
View all 21 lines...
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
01canSpawn = true
02print("Ready!")
03 
04function 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 - Vector3.new(0,1.4,-1)
10        part.FormFactor = "Custom"
11        part.Size=Vector3.new(0.75,0.75,0.75)
12        part.Material = "Neon"
13        part.TopSurface = "Smooth"
14        part.BottomSurface = "Smooth"
15        part.CanCollide = true
View all 24 lines...

Hope this helped you

0
Thanks and does this work if the parent (part) is not collideable? awesomemode14 68 — 5y
0
I think it does develop_d 53 — 5y

Answer this question