Error comes up even though script is working completely fine

I have scripted a clicking mouse tween animation for my clicker game, and my tween script works completely fine. However, whenever I run the animation, it comes up with this error in output. (Can only tween objects in the workspace.)

Here is an image of the error:

Here is my source code:
local Enabled = script.Parent.Enabled
local ClickParticle = script.Parent
local Players = game:GetService(“Players”)

while true do
	wait(0.1)
	if ClickParticle.Name == "ClickParticleCloned" then
		Enabled.Value = true
		if Enabled.Value == true then
			wait(1)
			ClickParticle:TweenPosition(
				UDim2.new(0.465, 0, 0.836, 0),
				"Out",
				"Sine",
				0.2,
				false	
			)
			
			wait(0.2)
			ClickParticle:Destroy()
		else
			Enabled.Value = false
		end
	else
		Enabled.Value = false
	end
end

Here is a recording of the animation:
AnimationVideo

Any help with this issue will be greatly appreciated.

This is most likely because the tweens aren’t finished when the objects are destroyed

Instead of having to fix this, you could just wrap all your code in a pcall and forget about it.

local success, err = pcall(function()
   -- your code here
end

It’s a bit of a hacky fix, but if your code works, I see no problem with it.

2 Likes

Thank you! This works a charm.