NPC dropping items multiple times? (Tycoon, NPC is a dropper)

I’m making a tycoon game where I have regular droppers as well as NPCs who walk from one point to another, and when they reach the conveyor belt they’re supposed to drop the item (they don’t do much else at the moment, they’re pretty much just fancy droppers that walk lol)

The regular droppers work as intended, but the NPC is spamming the items and is dropping 5 or 6 of them

(idk if the video works or not)

Here’s part of my code:

local tycoon = script.Parent.Parent.Parent.Parent
local dropperPartsFolder = tycoon:FindFirstChild("DropperParts")
local box = script.Parent.Parent.Parent.Parent.MainItems.Conveyor.box.stop1

local function WorkerDropper()
	local part = Instance.new("Part", dropperPartsFolder)
	part.Size = Vector3.new(1,1,1)

	part.Name = "DropperPart"
	part.CFrame = script.Parent.Spawner.CFrame

	game.Debris:AddItem(part, 20)
end

box.Touched:Connect(WorkerDropper)

I tried adding a wait() into the function in a few areas and it either broke the script completely or it caused some weird behavior. I tried looking through the forum and on Google but I couldn’t find any solutions. Is there a way to make sure the NPC only drops something one time per stop?

3 Likes

A very simple fix is to use a debounce but the actual root cause of this issue is because touched event fires every single time something makes a movement on the part

You can debounce by doing something like this:

local debounce = false

local function workerDropper()
if not debounce then
    local part = Instance.new("Part", dropperPartsFolder)
	part.Size = Vector3.new(1,1,1)

	part.Name = "DropperPart"
	part.CFrame = script.Parent.Spawner.CFrame

	game.Debris:AddItem(part, 20)
    debounce = true
    end
end

This ensures your code will only run once because then debounce gets set to true at the end of the scope then if the touched event gets fired again debounce will be true and it won’t run again.

1 Like

You didn’t set a debounce between touches.

Look at this. You trigger the box.Touched:Connect(WorkerDropper) event - which is intended to fire the function when the “box” is touched. The number of touches can be up to 100 per second by one character. When a character moves, it touches a part several times, which fires an event the number of times the part was touched. You need to set global debounce between each touch, or for each character separately (depending on their number).

local Part = script.Parent

local Debounce = false

Part.Touched:Connect(function(OtherPart)
	local Char = OtherPart.Parent
	
	if Char:IsA("Model") then
		if Char:FindFirstChild("Humanoid") then
			if Debounce == false then
				Debounce = true
				
				--Your script
				
				print("touched")
				
				wait(1) --Debounce time 
				
				Debounce = false
			end
		end
	end
end)

I hope I helped

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