Deleting Equipped Pet from player inventory and Datastore

I’m trying to delete an equipped pet on the server and then locally so it will save appropriately to datastore (so it doesn’t load the pet back the next time I play.) I decided to simply use a Kill button to delete the currently equipped pet so I don’t have to mess with new or existing GUI elements. The inventory is AlvinBlox’s petModule if that means anything to anyone.

My current thought is that I use a click connecter to start a local script, the script could unequip the pet and delete its local values and then fire a remote event to delete the values on the server as well. The datastore saves automatically when the player disconnects.

I’m super new to scripting so I’m not even sure this idea is possible.

My script is probably not even close, I’ve started from multiple different angles and I have issues no matter what route I take.

**Edited original scripts out, nobody needs to see that garbage but I will share them when I finish the process, although they are super ugly!

I’ve managed to create a function and remote event to delete a copy of the EquippedPet from the client and from the server, and delete the clone that’s actually with the player.

Now I just need to delete the frame of the pet in the pet inventory GUI in one of those functions. Here’s the code from the GUI section. I’m currently trying to figure out how to Destory() the frame that is “Equipped Pet”, but I’m not sure if that’s what I need to do because I’m still super noob.

local template = script:WaitForChild("Template")
local scrollingFrame = script.Parent:WaitForChild("Pets"):WaitForChild("ScrollingFrame")

local buttonConnections = {}

local function setTemplateEquipped(template)
	
	for i, v in pairs(scrollingFrame:GetChildren()) do
		if v:FindFirstChild("Equipped") then
			v.Equipped.Text = "UNEQUIPPED"
			v.Equipped.TextColor3 = Color3.fromRGB(255,0,0)
		end
	end
	
	template.Equipped.Text = "EQUIPPED"
	template.Equipped.TextColor3 = Color3.fromRGB(0,255,0)
end

local function addToFrame(pet)
	
	local newTemplate = template:Clone()
	newTemplate.Name = pet.Name
	newTemplate.PetName.Text = pet.Name
	newTemplate.Parent = scrollingFrame
	
	local newPet = pet:Clone()
	newPet.Parent = newTemplate.ViewportFrame
	
	local camera = Instance.new("Camera")
	camera.CFrame = CFrame.new(newPet.PrimaryPart.Position + (newPet.PrimaryPart.CFrame.lookVector * 3),newPet.PrimaryPart.Position)
	camera.Parent = newTemplate.ViewportFrame
	
	newTemplate.ViewportFrame.CurrentCamera = camera
	
	buttonConnections[#buttonConnections+1] = newTemplate.MouseButton1Click:Connect(function()
		if newTemplate.Equipped.Text == "EQUIPPED" then
			-- We know that as it is already equipped they want to unequip it
			game.ReplicatedStorage.UnequipPet:FireServer()
			newTemplate.Equipped.Text = "UNEQUIPPED"
			newTemplate.Equipped.TextColor3 = Color3.fromRGB(255,0,0)
		else
			-- We know that it is already UNEQUIPPEd and now they want to equip it
			game.ReplicatedStorage.EquipPet:FireServer(pet.Name)
			setTemplateEquipped(newTemplate)
		end
	end)
	
end

game.ReplicatedStorage.SendData.OnClientEvent:Connect(function(petNames)
	print("picked up on the client")
	for i, petName in pairs(petNames) do
		if game.ReplicatedStorage.Pets:FindFirstChild(petName) then
			addToFrame(game.ReplicatedStorage.Pets:FindFirstChild(petName))
		end
	end
end)

game.ReplicatedStorage.SetEquippedPet.OnClientEvent:Connect(function(petName)
	if scrollingFrame:FindFirstChild(petName) then -- Double check that the pet template has been created
		setTemplateEquipped(scrollingFrame[petName])
	end
end)

Any help is Super appreciated! I don’t want this to take an additional 2 weeks to figure out! :laughing:

Not sure if this helps:
Viewport-Frame