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

How do I on touch, only an item will clone every second?

Sorry for the silly title, anyways:

I want to clone one item every second if a player touches an object, but with this script, the object is cloned like crazy.

local spawner = game.Workspace.ToothpasteSpawner
local detector = script.Parent
local ball = game.ServerStorage.DeathBall

local function spawnBall (part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
		if humanoid then
		local ballClone = ball:Clone()
		wait(1)
		ballClone.Parent = game.Workspace
		ballClone.Position = spawner.Position
		wait(1)
	end
end

detector.Touched:Connect(spawnBall)

1 answer

Simply add a debounce. Here's your code with the debounce inserted.

local spawner = game.Workspace.ToothpasteSpawner
local detector = script.Parent
local ball = game.ServerStorage.DeathBall

local db=false
 
local function spawnBall (part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid and not db then
		db = true
		local ballClone = ball:Clone()
		ballClone.Parent = game.Workspace
		ballClone.Position = spawner.Position
		wait(1)
		db =false
	end
end
 
detector.Touched:Connect(spawnBall)

If I answer solved your problem please remember to accept it. It helps me out a lot!

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