Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What would be an efficient way to load pets in an inventory?

Basically what iv'e been trying to do is make a system where I use a datastore to store a players pets with a table. The way iv'e tried this is with a table. The issue that i'm hitting with this, however is with how I can load the pets.

With my current knowledge the only way I can do this would be a very long if then statement like the following:

if pet.ID == 1 then
"blah blah loading stuff"
else if pet.ID == 2 then
"more loading stuff"

This would obviously be long, and a real pain to work with if I have a lot of different kind of pets, and would take way to long to script. I don't know how else I could pull this off.

Anybody know what I could do?

1 answer

✓ Accepted answer

It’s best you store the pets’ names instead, as there isn’t much cross-referencing potential with digits unless there is an existing ID dictionary with each pet in it’s correlating key—though that is just a waste of space and ultimately tedious.

With using names, we can use :FindFirstChild() to locate a matching pet immediately! We can also appropriately condensate for non-existing pets with ease. To achieve this, we would need a folder in ServerStorage—for FE safety and optimization—containing all the pet models. Then, we build a function which can get us the pet we need in one line:

local ServerStorage = game:GetService("ServerStorage")
local AvailablePets = ServerStorage.Pets

local function GetPet(PetData)
    local FoundPet = AvailablePets:FindFirstChild(PetData)
    if (FoundPet) then
        return FoundPet:Clone()
    end
    return nil
end

local Pet = GetPet(PetData.Name)
Pet.Parent = --// Location

If this helps, don’t forget to accept this answer! If you have any further questions, feel free to comment below!

  • Feahren.

This discussion is preserved as it was. New answers, comments, and votes are closed.