Clone() function clones 2 parts instead of 1

Hi!
I’m trying to make a script that clones a tool (basically like throwing the object.). The problem is that it clones my tool 2 times instead of one time.

I don’t really see a problem with my script, because I added a debounce and all.

Anyways, here’s my script:

local touched = false
local TS = game:GetService("TweenService")
local clicked = false
game.ReplicatedStorage.Throw.OnServerEvent:Connect(function(plr, mouseHit)
	if not clicked then
		clicked = true
		local clone = plr.Character:FindFirstChildWhichIsA("Tool").Handle:Clone()
		plr.Character.Humanoid:LoadAnimation(script.Parent.Handle:FindFirstChild("Yeet")):Play()
		clone.Hitbox.Touched:Connect(function(hit)
			if not touched then
				touched = true
				clone.CanTouch = false
				if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= plr.Character and hit.Parent.Name ~= "Rope" and hit.Parent.Name ~= "Celestial Worm" then
					--print(hit.Parent)
					if hit.Parent.Humanoid.Health > 0 then					
						local target = hit.Parent
						local joints = target:GetDescendants()
						target.Humanoid.BreakJointsOnDeath = false
						game.ReplicatedStorage.YeetGain:FireClient(plr)
						local killed = false
						target.Humanoid.Died:Connect(function(killed)
							game.ReplicatedStorage.YeetGainDeath:FireClient(plr)
						end)
						print(target.HumanoidRootPart)
						target.HumanoidRootPart.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 160 + Vector3.new(0,160,0)
						print(target.HumanoidRootPart)
						clone.Impact:Play()
						print(hit.Parent.Name.." was hit by "..script.Parent.Name)
						--if not extraTouch then
						--	extraTouch = true
						target:FindFirstChild("Humanoid").Health -= 10
							--wait(1)
							--extraTouch = false
						--end
						for _,joint in pairs(joints) do
							if joint:isA("Motor6D") then						
								local socket = Instance.new("BallSocketConstraint")
								local att0 = Instance.new("Attachment")
								local att1 = Instance.new("Attachment")
								print(joint)
								target.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
								target.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
								target.Humanoid.AutoRotate = false
								--target.Humanoid.PlatformStand = true
								--target.Humanoid.WalkSpeed = 0
								--target.Humanoid.JumpPower = 0
								att0.Name = "Att0"
								att1.Name = "Att1"
								att0.CFrame = joint.C0
								att1.CFrame = joint.C1
								att0.Parent = joint.Part0
								att1.Parent = joint.Part1
								touched = true
								clone.CanTouch = false
		
								socket.Parent = joint.Part0
								socket.Attachment0 = att0
								socket.Attachment1 = att1
								att0.Name = "Att0"
								att1.Name = "Att1"
								
	
								socket.LimitsEnabled = true
								socket.TwistLimitsEnabled = true
		
								--wait(0.2)
		
								--wait(2)
		
								joint.Enabled = false
								
							end
						end
						print(target)
						print(target.Humanoid.Health)
			
						wait(0.2)
						clone:Destroy()
						wait(0.1)
						touched = false
						clone.CanTouch = true
						wait(0.9)
						if hit.Parent.Humanoid.Health > 0 then
							if target.Name ~= "Dummy3" then
								target.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
								target.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
								target.Humanoid.AutoRotate = true
								target.Humanoid.WalkSpeed = 26
								target.Humanoid.JumpPower = 7.5
								for _,joint2nd in pairs(joints) do
									if joint2nd:isA("Motor6D") then
										joint2nd.Enabled = true
									end	
								end
								for _, child in pairs(target:GetDescendants()) do
									if child.Name == "Att0" or child.Name == "Att1" or child:IsA("BallSocketConstraint") then
										child:Destroy()
									end	
								end
							end
						end		
					end				
				end
			end	
		end)	
	
		wait(0.4)
		plr.Character:FindFirstChildWhichIsA("Tool").Handle.Woosh:Play()
		wait(0.1)
		plr.Character:FindFirstChildWhichIsA("Tool").Handle.Transparency = 1
		clone.Transparency = 0
		local tool = clone.Parent
		clone.Parent = game.Workspace	
		clone.Anchored = true
		clone.CanCollide = false
		clone.Hitbox.CanCollide = false
		clone.CanTouch = true
		clone.Hitbox.CanTouch = true
		print(clone.Parent)
		
		--	clone.Position = script.Parent.Handle.Position
		clone.CFrame = plr.Character.HumanoidRootPart.CFrame+plr.Character.HumanoidRootPart.CFrame.LookVector * 5
		local Tween = TS:Create(clone,TweenInfo.new(0.5),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.LookVector * 80})
		Tween:Play()

		--print(mouseHit)
		wait(0.45)
		plr.Character:FindFirstChildWhichIsA("Tool").Handle.Transparency = 0	
		clone:Destroy()
		clicked = false
	end
end)	
--clone.Touched:Connect(function(hit)
	--local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	--print(plr,"was hit by",script.Parent.Name)
--end)

Any of your help is appreciated!

1 Like

You didnt make Clone properties like you did local tool = tool:clone()
And continued the script with this it won’t work

Try like this example
local Tool = workspace.Tool:Clone()
Tool.Parent = player.Backpack

Like this!
if It doesn’t work tell me
Also this is an example don’t do copy paste

This is not what I’m aiming for. I’m trying to make it so you throw the tool after someone. That is why I clone the tool into the workspace.

1 Like

But I do think I know the issue. I just don’t know how to fix it. Because it seems as if there’s 2 players having the tool it clones 2 times. I have a local script activating the script I showed you. They are both children of the tool, just for your information.

local tool = script.Parent
local plr = game.Players.LocalPlayer
local activate = false
tool.Activated:Connect(function()
	if not activate then
		activate = true
		game.ReplicatedStorage.Throw:FireServer(plr)
		wait(2)
		activate = false
	end
end)
1 Like

Nevermind. I now see what that variable controls.

Have you tried making the wait time longer to make sure it’s not firing the event many times? Also avoid using wait() it’s now depracated. Use task.wait() instead.

Are you positive the function is not fired twice?

Yes I quite am. I think it’s my local script that has something wrong in it. It’s probably the LocalPlayer, but I just don’t know what to replace it with.

Connecting events within a connection in this way is a terrible idea. Everytime the remote is fired, a new connection is made, and that’s where the issue is. You need to make sure it’s only connected once. The script is too messy for me to fix it.

So how would that look? Sorry, I have a hard time imagining how it would look like. So do I have to make a new remote event or what?

Could you please put a print() after the :FireServer and after the :Clone lines, try the game and then see if the events fire 2 times?

This definitely isn’t the main issue but quick note, there’s no point in editing an instance’s properties after they have been destroyed. The .CanTouch line won’t do anything

I’d also check if the clone has been destroyed before trying to change its properties in every line after the Touched event

This part might throw an error if the clone has already been destroyed

Then check if there 2 both removed and create a new one

My script never printed out any errors in the output.

And everything is working fine. I don’t see why those things would be the error.

Okay, I now know for sure that it’s because of the player amount, because I checked the workspace and it cloned 2 when 2 players and 1 when 1 player.

Does anyone have a solution for this

local Connection
Connection = – ur connection to when the tool activates
then do Connection:Disconnect() when youre done with the connection, it prevents the connection from making duplicates since the connection is ended after being used once