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:
function onTouched(hit) local partDrop = script.Parent local part = Instance.new("Part",partDrop) part.CFrame = partDrop.CFrame - Vector3.new(0,1.4,-1) part.FormFactor = "Custom" part.Size=Vector3.new(0.75,0.75,0.75) part.Material = "Neon" part.TopSurface = "Smooth" part.BottomSurface = "Smooth" part.CanCollide = true print("Dropped!") game.Debris:AddItem(part,math.random(2,5)) wait(6) end 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.
local debounce=false--declaring function onTouched(hit) if debounce==false then debounce=true--making it true local partDrop = script.Parent local part = Instance.new("Part",partDrop) part.CFrame = partDrop.CFrame - Vector3.new(0,1.4,-1) part.FormFactor = "Custom" part.Size=Vector3.new(0.75,0.75,0.75) part.Material = "Neon" part.TopSurface = "Smooth" part.BottomSurface = "Smooth" part.CanCollide = true print("Dropped!") game.Debris:AddItem(part,math.random(2,5)) wait(6) debounce=false--making it false end script.Parent.Touched:Connect(onTouched)
canSpawn = true print("Ready!") function onTouched(hit) if canSpawn then canSpawn = false local partDrop = script.Parent local part = Instance.new("Part",partDrop) part.CFrame = partDrop.CFrame - Vector3.new(0,1.4,-1) part.FormFactor = "Custom" part.Size=Vector3.new(0.75,0.75,0.75) part.Material = "Neon" part.TopSurface = "Smooth" part.BottomSurface = "Smooth" part.CanCollide = true print("Dropped!") game.Debris:AddItem(part,math.random(2,5)) wait(4) canSpawn = true print("Ready!") end end script.Parent.Touched:Connect(onTouched)
Hope this helped you