Function doesn't work as intended (duping)

I have this function that dupes a Sample and parents it to a frame list each time a Child is Added. The thing is, it keeps the original frames and duplicates them, but doesn’t delete the original ones

EXAMPLE:
(a, b, c) --original frames
FUNCTION HAPPENS
(a, a, b, b, c, c, d) --duplicates og frames then adds a new frame.

CODE:

local repSto = game:GetService("ReplicatedStorage")
local tweenServ = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local folderEmote = Player:WaitForChild("Emotes")
local emoteFolder = repSto.Animations
local emoteF = emoteFolder:GetChildren()
local sendEmote = repSto.sendEmote

local emote = script.Emote
local holder = script.Parent.Holder
local emoteList = holder.EmoteList
local emoteText = holder.TextLabel

function dupeEmote()
	local emoteB = {}
	
	for i, emote in pairs(folderEmote:GetChildren()) do
		local btn = script.Emote:Clone()
		local emoteN = emote.Name
		local emoteID = emote.AnimationId

		btn.Animation.AnimationId = emoteID
		btn.EmoteName.Text = emoteN

		local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
		local loadedAnim = char:WaitForChild("Humanoid"):LoadAnimation(emote)
		loadedAnim.Looped = true

		local char2 = game.Workspace:WaitForChild("Dummy"):Clone()
		char2.Parent = workspace
		local loadedAnim2 = char2.Humanoid:LoadAnimation(emote)
		loadedAnim2.Looped = true
		loadedAnim2:Play()
		char2.Parent = btn.ViewportFrame.WorldModel

		btn.Interact.Activated:Connect(function()
			if loadedAnim.IsPlaying then
				loadedAnim:Stop()
			else
				for i, playingEmote in pairs(animateEmotes) do
					playingEmote:Stop()
					table.remove(animateEmotes, i)
				end
				loadedAnim:Play()
				table.insert(animateEmotes, loadedAnim)
			end
		end)

		--animation
		char.Humanoid:GetPropertyChangedSignal('MoveDirection'):Connect(function()
			if char.Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
				loadedAnim:Stop()
			end
		end)

		table.insert(emoteB, btn)
	end
	
	for i, btn in pairs(emoteB) do
		btn.Parent = emoteList
	end
end

dupeEmote()
folderEmote.ChildAdded:Connect(dupeEmote)
folderEmote.ChildRemoved:Connect(dupeEmote)

Just to be clear, are you trying to just add a new frame and not affect the other frames, or are you trying to duplicate the old frames, then delete the old ones and add a new one? or something else?

Trying to add a new frame only.

what you could do is before adding every emote, use a function to check if that emote exists in the list using the ‘EmoteName’ label, and only continue the creation if it doesn’t exist, like this:

local repSto = game:GetService("ReplicatedStorage")
local tweenServ = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local folderEmote = Player:WaitForChild("Emotes")
local emoteFolder = repSto.Animations
local emoteF = emoteFolder:GetChildren()
local sendEmote = repSto.sendEmote

local emote = script.Emote
local holder = script.Parent.Holder
local emoteList = holder.EmoteList
local emoteText = holder.TextLabel

function findEmoteByName(name)
   for _,btn in pairs(emoteList:GetChildren())do
      if btn.EmoteName.Text == name then
         return btn
      end
   end
end

function dupeEmote()
	local emoteB = {}
	
	for i, emote in pairs(folderEmote:GetChildren()) do
        if not findEmoteByName(emote.Name) then
			local btn = script.Emote:Clone()
			local emoteN = emote.Name
			local emoteID = emote.AnimationId

			btn.Animation.AnimationId = emoteID
			btn.EmoteName.Text = emoteN
	
			local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
			local loadedAnim = char:WaitForChild("Humanoid"):LoadAnimation(emote)
			loadedAnim.Looped = true
	
			local char2 = game.Workspace:WaitForChild("Dummy"):Clone()
			char2.Parent = workspace
			local loadedAnim2 = char2.Humanoid:LoadAnimation(emote)
			loadedAnim2.Looped = true
			loadedAnim2:Play()
			char2.Parent = btn.ViewportFrame.WorldModel
	
			btn.Interact.Activated:Connect(function()
				if loadedAnim.IsPlaying then
					loadedAnim:Stop()
				else
					for i, playingEmote in pairs(animateEmotes) do
						playingEmote:Stop()
						table.remove(animateEmotes, i)
					end
					loadedAnim:Play()
					table.insert(animateEmotes, loadedAnim)
				end
			end)
	
			--animation
			char.Humanoid:GetPropertyChangedSignal('MoveDirection'):Connect(function()
				if char.Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
					loadedAnim:Stop()
				end
			end)
	
			table.insert(emoteB, btn)
        end
	end
	
	for i, btn in pairs(emoteB) do
		btn.Parent = emoteList
	end
end

dupeEmote()
folderEmote.ChildAdded:Connect(dupeEmote)
folderEmote.ChildRemoved:Connect(dupeEmote)

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