Clone Multiplies everytime script run

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

  1. Clone and Destroy

  2. After destroying the clone, running/using the script again multiplies the clone(adds another clone) every time

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShieldEvent = ReplicatedStorage.AbilitiesEvents:WaitForChild("ShieldEvent")
local DestroyShieldEvent = ReplicatedStorage.AbilitiesEvents:WaitForChild("DestroyShieldEvent")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")


ShieldEvent.OnServerEvent:Connect(function(player, Character, MousePosition, Name)		
	local Path = game.ReplicatedStorage.Powers:FindFirstChild(Name)
	local HumanoidRootPart = Character.HumanoidRootPart

	local Shield = Path.Shield:Clone()
	Shield.Parent = game.Workspace
	Shield.CFrame = HumanoidRootPart.CFrame

	local Weld = Instance.new("Weld", HumanoidRootPart)
	Weld.C0 = CFrame.new()
	Weld.C1 = CFrame.new()
	Weld.Part0 = HumanoidRootPart
	Weld.Part1 = Shield

	local ShieldTween = TweenService:Create(Shield, TweenInfo.new(1), {Size = Vector3.new(8, 8, 8), Transparency = 0})
	ShieldTween:Play()

	DestroyShieldEvent.OnServerEvent:Connect(function()

		local DestroyShieldTween = TweenService:Create(Shield, TweenInfo.new(1), {Transparency = 1})
		DestroyShieldTween:Play()
		DestroyShieldTween.Completed:Wait()

		Shield:Destroy()
		Weld:Destroy()
	end)
end)

While I spot one issue with the code here, I don’t think the multiplying is coming from this script, but rather the script firing it. For this one, your issue (minor) would be with the destroying event, which will add multiple connections and assumably cause errors. Simple fix for this is add a variable for the connection and disconnect the connection:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShieldEvent = ReplicatedStorage.AbilitiesEvents:WaitForChild("ShieldEvent")
local DestroyShieldEvent = ReplicatedStorage.AbilitiesEvents:WaitForChild("DestroyShieldEvent")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")


ShieldEvent.OnServerEvent:Connect(function(player, Character, MousePosition, Name)		
	local Path = game.ReplicatedStorage.Powers:FindFirstChild(Name)
	local HumanoidRootPart = Character.HumanoidRootPart

	local Shield = Path.Shield:Clone()
	Shield.Parent = game.Workspace
	Shield.CFrame = HumanoidRootPart.CFrame

	local Weld = Instance.new("Weld", HumanoidRootPart)
	Weld.C0 = CFrame.new()
	Weld.C1 = CFrame.new()
	Weld.Part0 = HumanoidRootPart
	Weld.Part1 = Shield

	local ShieldTween = TweenService:Create(Shield, TweenInfo.new(1), {Size = Vector3.new(8, 8, 8), Transparency = 0})
	ShieldTween:Play()
	
	local DestroyConnection

	DestroyConnection = DestroyShieldEvent.OnServerEvent:Connect(function()
		DestroyConnection :Disconnect()

		local DestroyShieldTween = TweenService:Create(Shield, TweenInfo.new(1), {Transparency = 1})
		DestroyShieldTween:Play()
		DestroyShieldTween.Completed:Wait()

		Shield:Destroy()
		Weld:Destroy()
	end)
end)

Would you provide the script that fires the event?

Local Script

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShieldEvent = ReplicatedStorage.AbilitiesEvents:WaitForChild("ShieldEvent")
local DestroyShieldEvent = ReplicatedStorage.AbilitiesEvents:WaitForChild("DestroyShieldEvent")
local Tool = script.Parent

Tool.Equipped:Connect(function()
	local Name = player.Character.CharacterName.Value
	local LeftCharge = game.ReplicatedStorage.Powers:FindFirstChild(Name).Charge.Charge:Clone()
	local RightCharge = game.ReplicatedStorage.Powers:FindFirstChild(Name).Charge.Charge:Clone()
	LeftCharge.Parent = Character.LeftHand
	RightCharge.Parent = Character.RightHand
	
	local RandomAnimation = script:GetChildren()[math.random(1, #script:GetChildren())]
	local ChargingAnimation = player.Character.Humanoid:LoadAnimation(RandomAnimation)
	local ShieldAnimation = player.Character.Humanoid:LoadAnimation(script.Parent.ShieldAnimation)
	ChargingAnimation:Play()
	
	Tool.Activated:Connect(function()
		ChargingAnimation:Stop()
		ShieldAnimation:Play()
	
		local MousePosition = mouse.Hit.Position
		ShieldEvent:FireServer(Character ,MousePosition, Name)
	end)

	Tool.Unequipped:Connect(function()
		DestroyShieldEvent:FireServer()
		
		LeftCharge:Destroy()
		RightCharge:Destroy()

		ChargingAnimation:Stop()
		ShieldAnimation:Stop()
	end)
end)

You need to disconnect your Activated event on the tool

local activatedConnection
local unequippedConnection

activatedConnection = Tool.Activated:Connect(function()
	ChargingAnimation:Stop()
	ShieldAnimation:Play()

	local MousePosition = mouse.Hit.Position
	ShieldEvent:FireServer(Character ,MousePosition, Name)
end)

unequippedConnection = Tool.Unequipped:Connect(function()
	activatedConnection:Disconnect()
	unequippedConnection:Disconnect()
	DestroyShieldEvent:FireServer()
	
	LeftCharge:Destroy()
	RightCharge:Destroy()

	ChargingAnimation:Stop()
	ShieldAnimation:Stop()
end)
2 Likes

This fixed the cloning problem, tysm

1 Like

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