part:Clone( ) is cloning the part more times than wanted

I am trying to clone a part from replicated storage and into the workspace using Part:Clone(), and then destroying the part after I tween it.

The issue I am facing is that when I clone the part it clones multiple times as in, the first time I use it it produces the part once, the second time I use it, it produces two parts instead of one every time I use the ability.

The tweening isn’t the issue, Ive tried it with and without tweens and the issue remains.

Heres how my script is currently set up.
Local script inside starter character scripts

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.E and gameProcessedEvent == false and os.clock() -
firefliesCoolDown >= 5 then

	local mousepos = mouse.Hit.Position
	local humRPpos = humanoidRootPart.Position
	
	local distance = ((humRPpos - mousepos).Magnitude)
	
	task.wait()
	humanoidRootPart.Anchored = true
	firefliesAnim:Play()
	firefliesAnimBegin:FireServer(mousepos, distance)
	
	firefliesAnim.Ended:Connect(function()
		firefliesAnimEnd:FireServer(mousepos, distance)
		firefliesCoolDown = os.clock()
		task.wait(1)
		humanoidRootPart.Anchored = false

	end)
end

end)

Serverscript inside of serverscriptservice

firefliesAnimEnd.OnServerEvent:Connect(function(player, mousepos, distance)

local firefliesClone = fireflies:Clone()

firefliesClone.Parent = game.Workspace

firefliesClone.Position = player.Character.HumanoidRootPart.Position + Vector3.new(-3,1,1)

local goals = 
{
		Position = mousepos	
	}


local tweenInfo = TweenInfo.new(
	distance/50,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.In,
	0,
	false,
	0
)

local firefliesTween = tweenService:Create(firefliesClone,tweenInfo, goals)

firefliesTween:Play()

firefliesTween.Completed:Connect(function()
firefliesClone.ParticleEmitter:Emit(100)
task.wait(0.95)
firefliesClone:Destroy()
firefliesTween:Destroy()
end)

end)

If what I’m saying is confusing and you need clarification tell me and I can try and explain it in a more understandable manner.

Edit: here is my issue recorded… https://youtu.be/6_97Xj3GDnk

2 Likes

Well, it looks like the issue you may be facing is that the firefliesAnimEnd event in your ServerScriptService is firing multiple times, causing the fireflies:Clone() operation to be executed multiple times and resulting in the creation of multiple parts. This is why you’re seeing multiple clones of the part in your workspace.

1 Like

try adding a debounce to your code when running it

I created a cooldown system using os.clock() so i’m not sure if its firing multiple times but I will try it and see if it fixes the issue

Not enough code to really test anything here … so, maybe this will help.

local anim = true
userInputService.
      --	
    firefliesCoolDown >= 5 then
    if anim then anim = false
      --
      --
    humanoidRootPart.Anchored = false
    anim = true
    --

Could even test off the lock …

firefliesCoolDown >= 5 then
if humanoidRootPart.Anchored = false then

You’re right, the anim ending fires twice and it clones it twice as well, it seems the cooldown i created only effects the anim beginning.

Your idea also works, thanks for replying

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