How to delete unused moves in moveset?

My script makes it so that if the script’s name is the same as the move’s name, it will parent itself to the move.

What I’m trying to achieve is that if the move is not found, the move will be deleted.

For example, if Move1 and Move5 are parented, then Move2, 3, and 4 will be removed.
But my issue is that Move1 and Move5 think they are not the same and they remove each other instead.

This is my code right now:

Remotes.ApplyMoveset.OnClientEvent:Connect(function(ChrName)
    -- The moveset GUI being cloned.
	local NewMoveset = MovesetGUI:Clone()

	for i, v in MoveFolder:FindFirstChild(ChrName):GetChildren() do
		for _, x in NewMoveset.Frame:GetChildren() do
			if v.Name == x.Name then
                -- If the move's name is the same as the script's name, the script will parent itself to the move.
				local Move = v:Clone()
				Move.Parent = x
				Move.Disabled = false
			elseif v.Name ~= x.Name and x:IsA('TextButton') then
                -- If the move's name is not the same to the script's name, it will get deleted.
				print(v.Name.." is not the same with "..x.Name)
				x:Destroy()
			end
		end
	end
    -- Parent the GUI to the player
	NewMoveset.Parent = Player.PlayerGui

end)

Any help is appreciated, thank you.

It looks like you’re trying to loop through the children of MoveFolder:FindFirstChild(ChrName) and NewMoveset.Frame, and if the name of the child of MoveFolder matches the name of the child of NewMoveset.Frame, you want to clone the child of MoveFolder and parent it to the child of NewMoveset.Frame. However, if the names don’t match, you want to delete the child of NewMoveset.Frame.

Here’s one way you could achieve this:

Remotes.ApplyMoveset.OnClientEvent:Connect(function(ChrName)
    -- The moveset GUI being cloned.
	local NewMoveset = MovesetGUI:Clone()

    -- Create a table to store the names of the children of MoveFolder
    local moveNames = {}
	for i, v in MoveFolder:FindFirstChild(ChrName):GetChildren() do
        -- Store the names of the moves in the moveNames table
        table.insert(moveNames, v.Name)
	end

	for _, x in NewMoveset.Frame:GetChildren() do
        -- If the child of NewMoveset.Frame is not in the moveNames table, delete it
        if not table.find(moveNames, x.Name) then
            x:Destroy()
        end
	end

    -- Loop through the children of MoveFolder again
	for i, v in MoveFolder:FindFirstChild(ChrName):GetChildren() do
		for _, x in NewMoveset.Frame:GetChildren() do
            -- If the names match, clone the child of MoveFolder and parent it to the child of NewMoveset.Frame
			if v.Name == x.Name then
				local Move = v:Clone()
				Move.Parent = x
				Move.Disabled = false
			end
		end
	end
    -- Parent the GUI to the player
	NewMoveset.Parent = Player.PlayerGui

end)
2 Likes

Wow! Thank you so much, this works!

1 Like

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