Glitch when creating more than one union to workspace(Fixed)

Hi developers, so this script is working fine, until I add two unions to the workspace.
The touched event is working, but the animation that’s setting the position of the heart up and down is not.
By the way I’m new to OOP.

Module script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Heart = ReplicatedStorage.Heart

local number = 1

local heart = {}
heart.__index = heart

local function update(self)
	if (self.Object) then
		self.Object.Position = self.Position
	end
end

local function touched(self)
	local object = self.Object
	local connection
	
	connection = object.Touched:Connect(function(hit)
		local player = (Players:GetPlayerFromCharacter(hit.Parent))

		if (hit) and (hit.Parent) and (player) then
			local stop = false

			task.delay(2.5,function()
				stop = true
			end)

			self.StopEffect = true

			task.wait(0.5)

			local speed = 1

			repeat
				object.Position += Vector3.new(0,0.025,0)
				object.Orientation += Vector3.new(0,speed,0)
				task.wait(0.05)
				if speed <= 10 then
					speed += 1
				end
			until (stop) or (not object)

			for i = 0,1,0.1 do
				object.Transparency = i
				task.wait(0.05)
			end
			
			self:Destroy()
			connection:Disconnect()
		end
	end)
end

function heart.new(pos)
	local self = setmetatable(heart,{})
	self.Object = Heart:Clone()
	self.Name = "Heart"..tostring(number)
	self.Position = pos
	self.Parent = workspace
	
	print(self.Name)
	
	self.Object.Position = self.Position
	self.Object.Name = self.Name
	self.Object.Parent = self.Parent
	self.StopEffect = false
	
	coroutine.wrap(function()
		while true do
			if (self.Object) and (not self.StopEffect) then
				local oldPos = pos
				
				repeat
					self.Position += Vector3.new(0,0.05,0)
					coroutine.wrap(update)(self)
					task.wait(0.01)
				until (self.Position.Y >= (oldPos.Y + 0.5)) or (not self.Object) or (self.StopEffect)
				
				task.wait(0.05)
				
				repeat
					self.Position -= Vector3.new(0,0.05,0)
					coroutine.wrap(update)(self)
					task.wait(0.01)
				until (self.Position.Y <= (oldPos.Y - 0.5)) or (not self.Object) or (self.StopEffect)
			else
				break
			end
		end
	end)()
	
	coroutine.wrap(touched)(self)
	
	number += 1
	
	return self
end

function heart:Destroy()
	local Object = self.Object
	
	if Object then
		self.Object = nil
		Object:Destroy()
	end
end

return heart

Script:

local heart = require(script.Heart)

local pos = Vector3.new(0,2,0)

for i=0,9,1 do
	heart.new(pos+Vector3.new(0,0,i*3))
end

If I would type:

local heart = require(script.Heart)

local pos = Vector3.new(0,2,0)

heart.new(pos)

it would work, but I want more unions/hearts.

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