Cloning pets templates

Hi, so, I have a pet system with GUI and I was wondering : Is there any problems if I clone the pets-templates in server-side? I’ve already tried locally put server couldn’t see the template being selected so it was erroring “nil”.

1 Like

My pet system is server-sided. The client requests equip/unequip of pets via a GUI and the server executes the request if the player owns the pet. However, we need more details regarding the “pets-templates”. Are these Gui templates or the actual pet models and we also need to see your code to understand your exact issue that you need support on.

:octopus:

A pet template is this
image
So, I was wondering if I should clone them server-sided then parent them to player gui directly[into the good screengui, ofcourse]

If I were doing it, I would keep the templates in a folder in ReplicatedStorage (which both server and client can reach) and clone them into the PlayerGui via a LocalScript. Any sort of work like this that only affects the client should be done on the client in a LocalScript.

Now, if you unlock a new pet, you are going to need to use networking between the server and client. RemoteEvents and RemoteFunctions are something you should look into. Also, if your template allows you to equip/unequip you will want to fire or invoke the server to equip/unequip the real pet model from the player.

1 Like

Oh, don’t worry about equipping, I have my own way. Also, is it efficient to destroy every template and generate another ones instead when u unlock a pet/delete one? I’m actually doing that :

			for i, template in pairs(MainGui.PetInventory.PetList:GetChildren()) do
				if template:IsA("Frame") then
					template:Destroy()
				end
			end

That code is “efficient” in the sense that is does not have a complex run-time. However, I do not think it is logical to delete all of the existing frames when you unlock or delete just one pet. You should just delete that one pet template and tell the server to update the player’s data to reflect that the pet has been deleted.

What I do is invoke the server with RemoteFunctions when I want to buy or delete a pet. The server checks if the player truly owns the pet and if they do, the RemoteFunction returns true to the client and deletes that one pet or adds it to the GUI.

It’s kinda hard to keep track of pets when they are duplicated too, that’s why I’m just doing that simple mechanic.

What I do is keep a table on the server that looks like this:

local playerDataTemplate = {
     OwnedPets = {
          ["Pet1Name"] = 1,
          ["Pet2Name"] = 2
     }
}

When you request a deletion, the server makes sure the player has 1 or more of that pet then decrements the value and tells the client to delete that pet template on the client’s GUI. If you get a new pet of that type, it increments the pet in their server-side data and tells the client’s GUI to add another pet template.

Oh, so you’re using an actual folder of Roblox instances. Well, in my opinion that actually makes it easier because instead of keeping the physical pet model instances in the folder you could put a NumberValue inside of the folder where the name is set to the same name as the actual pets (which reside somewhere else), and the NumberValue.Value is equal to the amount of pets that you own.

But, this is a more complicated implementation, so do what you can for now and improve as you go. Good luck!

1 Like

Already doing that, thank you for the advice! I also have an IntValue for how many actual pets are equipped. Thank you again and cya!

Hey, so looking at the code you sent I can recommend something, try using a RemoteFunction to return the given data (using a table), so the client can create the template instead of the server creating them as it could create a performance issue for the server and using the client to create is a more reasonable and faster alternative rather than creating on the server!

I’ve tried this but when I send the template object, it says it’s nil since that template is only created locally.