Challenge spawning many ducks at once?

Hello. I’ve been trying to make a Duck-Popper Minigame, but whenever it starts, it spawns WAY to many ducks! It’s meant to spawn 3, but it spawns about 6, then about 12. How can I stop this?

Script in Trigger, which starts the chat with an NPC, then starts the game:

script.Parent.Touched:Connect(function(hit)
	game.ReplicatedStorage.Gambling.TouchedTrigger:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent), "Line1")
end)

game.ReplicatedStorage.Gambling.SendNextEvent.OnServerEvent:Connect(function(player)
	print("starting...")
	wait(3)
	player.Character.HumanoidRootPart.CFrame = CFrame.new(-112.1, 5, -47.2)
	game.ReplicatedStorage.Gambling.StartedGame:FireClient(player)
	wait(1)
	script.Parent.Parent.StartGame:Fire()
end)

Script in Minigame that is meant to do many things:

local parent = script.Parent
local duckWH = game.ReplicatedStorage.RubberDuckies.RubberDuckWithHat
local duck = game.ReplicatedStorage.RubberDuckies.RubberDuck

local duckType = 0
local stripToUse = 0
local amountNeeded = 0

function generateNumbers()
	print("started!")
	local determineDT = math.random(1, 5)
	local determineSTU = math.random(1,2)
	duckType = determineDT
	stripToUse = determineSTU
	if duckType <= 4 then
		print("Type is Normal Rubber Ducky!")
		amountNeeded = 3
	elseif duckType == 5 then
		print("Type is Top Hat Rubber Ducky!")
		amountNeeded = 1
	end
	wait(1)
	generateDucks(amountNeeded)
end

function killDuck(duckToKill)
	local smoke = Instance.new("Smoke", duckToKill)
	wait(1)
	duckToKill:Destroy()
end

function generateDucks(amountToGen)
	if duckType <= 4 then
		local newDuck = duck:Clone()
		newDuck.Parent = parent
		newDuck.Head.CFrame = parent:FindFirstChild("Spawn" .. stripToUse).CFrame
		local mDetector = Instance.new("ClickDetector", newDuck.Head)
		mDetector.MouseClick:connect(function()
			killDuck(newDuck)
			parent.Scores.Score.Value = parent.Scores.Score.Value + 100
			parent.Scores.DucksEliminated.Value = parent.Scores.DucksEliminated.Value + 1
		end)
		wait(2)
		local newDuck2 = duck:Clone()
		newDuck2.Parent = parent
		newDuck.Head.CFrame = parent:FindFirstChild("Spawn" .. stripToUse).CFrame
		local mDetector2 = Instance.new("ClickDetector", newDuck2.Head)
		mDetector.MouseClick:connect(function()
			killDuck(newDuck)
			parent.Scores.Score.Value = parent.Scores.Score.Value + 100
			parent.Scores.DucksEliminated.Value = parent.Scores.DucksEliminated.Value + 1
		end)
		wait(2)
		local newDuck3 = duck:Clone()
		newDuck3.Parent = parent
		newDuck.Head.CFrame = parent:FindFirstChild("Spawn" .. stripToUse).CFrame
		local mDetector3 = Instance.new("ClickDetector", newDuck3.Head)
		mDetector.MouseClick:connect(function()
			killDuck(newDuck)
			parent.Scores.Score.Value = parent.Scores.Score.Value + 100
			parent.Scores.DucksEliminated.Value = parent.Scores.DucksEliminated.Value + 1
		end)
	elseif duckType == 5 then
		local newDuck = duckWH:Clone()
		newDuck.Parent = parent
		newDuck.TopHat.CFrame = parent:FindFirstChild("Spawn" .. stripToUse).CFrame
		local	 mDetector = Instance.new("ClickDetector", newDuck.Head)
		mDetector.MouseClick:connect(function()
			killDuck(newDuck)
			parent.Scores.Score.Value = parent.Scores.Score.Value + 500
			parent.Scores.DucksEliminated.Value = parent.Scores.DucksEliminated.Value + 1
		end)
	end
end

parent.StartGame.Event:Connect(function()
	generateNumbers()
end)

Any help is appreciated. They’re both ServerScripts placed as Descendants of Workspace.

Here’s an example of the Minigames Model and requirements:

Example.rbxl (62.7 KB)

1 Like

Have you implemented any form of debounce? It looks like the function is triggered after a .Touched event, which can fire several times when a player walks onto a part.

Hey there!

So that .Touched event is only for the dialog.

the client must be firing SendNextEvent more than once.

Makes sense!

I have a print line in place that says, “starting…”, and that pops up 5 times. I’m gonna try to resolve this issue now.