While loop creates part, but creates about 6 at the same time

So I’m creating a mini-game where a part spawns and the character clicks it and gets points. I already have it tween to the new position, and I made a loop so it would create more than one part so the player can get a bunch of points. The problem is that my loop is creating like 6 of the same part, then tweens to the new position and creates a bunch more. I’ve tried putting wait() throughout the entire script and it did nothing.

local partDestroyer = game.Workspace["Part  Destroyer"]
local partEmit = game.Workspace.PartEmmitter
local debounce = false
local range = 1
script.Parent.Touched:Connect(function()
	while true and task.wait(.5) do
		local clickPart = Instance.new("Part")
		local clickDetector = Instance.new("ClickDetector")	
		print("Part emit")

		clickDetector.Parent = clickPart
		clickDetector.MaxActivationDistance = 10000
		clickPart.Parent = workspace
		clickPart.Anchored = true
		clickPart.Size = Vector3.new(8.059, 7.04, 8.577)
		clickPart.Color = Color3.new(1, 0.0627451, 0.423529)
		clickPart.Position = partEmit.Position + Vector3.new(math.random(-range, range), math.random(-range, range), math.random(-range, range))
		clickPart.Name = "clickPart"

		local tweenService = game:GetService("TweenService")
		local tweenInfo = TweenInfo.new(3)
		local goal = {}

		goal.Position = Vector3.new(clickPart.Position.X + 134, clickPart.Position.Y, clickPart.Position.Z)

		local tween = tweenService:Create(clickPart,tweenInfo, goal)

		tween:Play()
		clickDetector.MouseClick:Connect(function()
			clickDetector.Parent:Destroy()
		end)
		task.delay(3, function() clickPart:Destroy() end)
	end
end)

Here’s the code. I’m sure it’s something with the loop because everything functioned fine before I tried making it create multiple parts. Apologies for the messy code, I still have some old things that I haven’t removed while editing it.

I think you forgot to implement the ‘debounce’ variable

local partDestroyer = game.Workspace["Part  Destroyer"]
local partEmit = game.Workspace.PartEmmitter
local debounce = false
local range = 1
script.Parent.Touched:Connect(function()
if debounce then return end
debounce = true
	while true and task.wait(.5) do
		local clickPart = Instance.new("Part")
		local clickDetector = Instance.new("ClickDetector")	
		print("Part emit")

		clickDetector.Parent = clickPart
		clickDetector.MaxActivationDistance = 10000
		clickPart.Parent = workspace
		clickPart.Anchored = true
		clickPart.Size = Vector3.new(8.059, 7.04, 8.577)
		clickPart.Color = Color3.new(1, 0.0627451, 0.423529)
		clickPart.Position = partEmit.Position + Vector3.new(math.random(-range, range), math.random(-range, range), math.random(-range, range))
		clickPart.Name = "clickPart"

		local tweenService = game:GetService("TweenService")
		local tweenInfo = TweenInfo.new(3)
		local goal = {}

		goal.Position = Vector3.new(clickPart.Position.X + 134, clickPart.Position.Y, clickPart.Position.Z)

		local tween = tweenService:Create(clickPart,tweenInfo, goal)

		tween:Play()
		clickDetector.MouseClick:Connect(function()
			clickDetector.Parent:Destroy()
		end)
		task.delay(3, function() clickPart:Destroy() end)
	end
end)

Hopefully this should work

Thank you!! I completely forgot about the debounce.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.