Built in function cloning part 8 times rather than once

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a script where when a player touches a brick, a part from Lighting is cloned and the clone part is placed in the workspace.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is when I stand on the part, 8 parts generate instead of one.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking up tutorials and adding wait times. I’ve looked on the developer hub.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		wait(1)
		local PartTwoClone = game.Lighting.PartTwo:Clone()
		PartTwoClone.Parent = game.Workspace
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You don’t have a debounce, leading to touched firing and executing the same code many times per touch. Wait(1) won’t do anything as connections are asynchronous.

1 Like

what you need is a debounce in the script.

local debounce = false
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and debounce == false then
                debounce = true
		wait(1)
		local PartTwoClone = game.Lighting.PartTwo:Clone()
		PartTwoClone.Parent = game.Workspace
                debounce = false
	end
end)

that should stop it from running 8 times

2 Likes

You should add a debounce instead of that wait(1)
Here is the part you are missing

local deb = false

if hit.Parent:FindFirstChild("Humanoid") and not deb then
deb = true
wait(1)
--Your code
deb = false
end
1 Like

You are touching it multiple times, wait(1) won’t help, you need to add debounce

local db = false
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') and db ~= true then
		db = true
		local PartTwoClone = game.Lighting.PartTwo:Clone()
		PartTwoClone.Parent = game.Workspace
		wait(1)
		db = false
	end
end)
1 Like