Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 answers

✓ Accepted answer

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

2 comments

0

Thanks and does this work if the parent (part) is not collideable?

0

I think it does

This discussion is preserved as it was. New answers, comments, and votes are closed.