Cloning too many objects

I made it where when a certain part interacts with another part the certain part gets destroyed and then cloned somewhere else. Its complicated to explain but isn’t when I show you what I mean.

Here I have a clip of my issue. Sorry for poor quality:

So in the video it works the first time as the ball rolls down, but the second time it clones multiple duplicates of the part for so reason when its only supposed to clone it once.

The only script I have for this is one in StarterPlayerScripts which is a local script. Then I also have a model in Workspace called Course which contains six parts called Connect, connect2, connect3, and so on. Last I have a part (which is the ball) called Snowball in ReplicatedStorage. Here is the local script in StarterPlayerScripts:

local Snowball = game.ReplicatedStorage.Snowball:Clone()

Snowball.Parent = game.workspace.CurrentCamera


local Connect = game.Workspace.Connect
local Connect2 = game.Workspace.Course.Connect2
local Connect3 = game.Workspace.Course.Connect3
local Connect4 = game.Workspace.Course.Connect4
local Connect5 = game.Workspace.Course.Connect5
local Connect6 = game.Workspace.Course.Connect6
local Players = game:GetService("Players")
local Snowball = game.Workspace.Camera.Snowball
local TouchDestination16 = game.Workspace:WaitForChild("TouchDestination16")

Connect.Touched:connect(function(hit)
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
		Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = TouchDestination16.CFrame
	end
end)

Connect2.Touched:connect(function(hit)
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
	end
end)

Connect3.Touched:connect(function(hit)
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
	end
end)

Connect4.Touched:connect(function(hit)
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
	end
end)

Connect5.Touched:connect(function(hit)
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
	end
end)

Connect6.Touched:connect(function(hit)
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
	end
end)

Too sum it up basically all I want is when the ball hits any of the six connect pieces it gets destroyed and then cloned back up at the start. Thanks!

1 Like

It gets touched multiple times before destroying the snowball, so just add a debounce so it can only be touched once.

local db = false

Connect.Touched:connect(function(hit)
    if db ~= false then return end
    db = true
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		local Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
		Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = TouchDestination16.CFrame
	end
    task.wait(1)
    db = false
end)
2 Likes

Thanks but that didn’t seem too work. Its also not giving me any errors.

1 Like

So yes add a denounce, but also to make things more simplified, use a for loop to set all the Connect events

2 Likes

How would I do that? I’m just starting scripting sorry.

1 Like

Assuming that there’s only those connections in ‘game.Workspace.Course’, you’d want to do something like this:

for _,connection in pairs(game.Workspace.Course:GetChildren()) do -- For extra help: use [this](https://devforum.roblox.com/t/could-somebody-explain-for-i-v-in-pairs/455868)
	connection.Touched:connect(function(hit)
		if hit.Name == "Snowball" then
			Snowball:Destroy()
			local Snowball = game.ReplicatedStorage.Snowball:Clone()
			Snowball.Parent = game.workspace.CurrentCamera
		end
	end)
end

You’d still have to make the touched function for ‘game.Workspace.Connect’ separate as it does an extra line.

1 Like

I forgot but yeah you’d want to use miles updated function

1 Like

It seems like everything is working except it does not destroy the snowball when it interacts with the connect part.

Try adding a count, example:

local count = 0
if count == 0 then
count = 1
--clone
count = 0
end
1 Like

in the touched event remove the local for local Snowball.

1 Like

Alright I removed the local. It works how I want it except the snowball gets destroyed and respawned every time it interacts with any part. I only want it to be destroyed when it hits a neon red part.

Here is also the script that I am using:

local Snowball = game.ReplicatedStorage.Snowball:Clone()

Snowball.Parent = game.workspace.CurrentCamera

local db = false

local Connect = game.Workspace.Connect
local Connect2 = game.Workspace.Course.Connect2

local Players = game:GetService("Players")
local Snowball = game.Workspace.Camera.Snowball
local TouchDestination16 = game.Workspace:WaitForChild("TouchDestination16")

for _,Connect in pairs(game.Workspace.Course:GetChildren()) do 
	Connect.Touched:connect(function(hit)
	if db ~= false then return end
	db = true
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
		Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = TouchDestination16.CFrame
		end
	task.wait(1)
	db = false
	end)
end

for _,Connect2 in pairs(game.Workspace.Course:GetChildren()) do 
	Connect2.Touched:connect(function(hit)
	if db ~= false then return end
	db = true
	if hit.Name == "Snowball" then
		Snowball:Destroy()
		Snowball = game.ReplicatedStorage.Snowball:Clone()
		Snowball.Parent = game.workspace.CurrentCamera
	end
	task.wait(1)
	db = false
	end)
end