Accidentally cloning more than needed

  1. **What do you want to achieve?
    the script is for a stomp ability that is inside a tool
    I want the script to only duplicate the instances once when its used

  2. **What is the issue?
    when the “stomp” event is fired the script clones Particle Emitters on the character for effects., it works fine the first time. but when I use the ability the second time it clones 2, the third time it will clone 3 (etc) which leads to many problems for me

  3. What solutions have you tried so far?
    I have tried moving the stomp server function out of the charge server function, and use coroutine.wrap to let them run at the same time. but that keeps giving me a studio error.

Here are the scripts, they are both inside the game.StarterPack and descendants of a tool.

Local Script

local cd = false
local cdTime = 5

local equipped = false
local animation = script:WaitForChild("Animation")




script.Parent.Equipped:Connect(function()
	equipped = true
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
end)


script.Parent.Activated:Connect(function()
	if equipped == true then
		if cd == false then
			cd = true
			
			local char = script.Parent.Parent
			local humanoid = char:WaitForChild("Humanoid")
			local charge = script.Parent:WaitForChild("Charging")
			local stomp = script.Parent:WaitForChild("Stomp")
			
			local anim = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
			anim:Play()
			
			anim:GetMarkerReachedSignal("Charge"):Connect(function(parameter)			
				print(parameter)
				charge:FireServer()
				humanoid.WalkSpeed = 0
			end)
			
			anim:GetMarkerReachedSignal("Stomp"):Connect(function(parameter)
				print(parameter)
				stomp:FireServer()
				wait(.5)
				humanoid.WalkSpeed = 16
			end)
			
			wait(cdTime)
			cd= false
		end
	end
end)

Server Script


local charge = script.Parent:WaitForChild("Charging")
local stomp = script.Parent:WaitForChild("Stomp")

charge.OnServerEvent:Connect(function()
	print("Charge Event Fired")
	local char = script.Parent.Parent
	local humanoid = char:WaitForChild("Humanoid")

	local chargePart = game.ServerStorage.StompFX.ChargingBlock:Clone()
	chargePart.Parent = workspace.Fx
	chargePart.CFrame = char.Torso.CFrame
	chargePart.Charging:Emit()

	stomp.OnServerEvent:Connect(function()
		print("Stomp Event Fired")
		local rLeg = char:WaitForChild("Right Leg")
		local stompAttachment = game.ServerStorage.StompFX.AttachmentHolder:Clone()
		stompAttachment.Parent =char
		stompAttachment.Stomp.Parent = rLeg
		rLeg:WaitForChild("Stomp").Position = Vector3.new(0,-0.9,0)	
		rLeg.Stomp.SmokeRing:Emit(80)
		rLeg.Stomp.StompParticles:Emit(500)
		rLeg.Stomp.Crack:Emit(1)
	end)

end)

Thanks in advance for your time, its much appreciated!

Just move stomp.OnServerEvent:Connect(function() outside of the charge.OnServerEvent:Connect(function() like this:

harge.OnServerEvent:Connect(function()
	print("Charge Event Fired")
	local char = script.Parent.Parent
	local humanoid = char:WaitForChild("Humanoid")

	local chargePart = game.ServerStorage.StompFX.ChargingBlock:Clone()
	chargePart.Parent = workspace.Fx
	chargePart.CFrame = char.Torso.CFrame
	chargePart.Charging:Emit()
end)

stomp.OnServerEvent:Connect(function()
	print("Stomp Event Fired")
	local rLeg = char:WaitForChild("Right Leg")
	local stompAttachment = game.ServerStorage.StompFX.AttachmentHolder:Clone()
	stompAttachment.Parent =char
	stompAttachment.Stomp.Parent = rLeg
	rLeg:WaitForChild("Stomp").Position = Vector3.new(0,-0.9,0)	
	rLeg.Stomp.SmokeRing:Emit(80)
	rLeg.Stomp.StompParticles:Emit(500)
	rLeg.Stomp.Crack:Emit(1)
end)

The cause of your problem is that :Connect links an event to a function. So each time you charge your move, the function containing “Stomp Event Fired” is connected to the event when the player stomps. So by charging your move three times, the stomp function is connected to the event three times.