How do you create pets, with experience?

How do you create a datastore in which pets have individual levels and experience points?

I know this is how you could create pets in a datastore:

Data.pets = {}
for pet,level in pairs(playerPetTable) do
      Data.pets[pet]=level
end
1 Like

Using a table infrastructure is definitely your best bet. It’s easy to store new information and to be able to access/update said information.

You could create a function that’s used for creating a new pet in the player’s inventory table (server-sided stuff)

Here’s an example of how that could look:

local function AddPet(player, petToAdd)
local pet_object = {}
pet_object.Level = 0
pet_object.XP = 0
pet_object.XP_Goal = 50
--//Add other fields depending on what kind of pet we're creating
table.insert(player_data[player.Name].pets, pet_object) --//Just an example of how you could add this pet to their data table
end

And then, you could store pet information in a dictionary for each type of pet, so when we call this function it can look up that pet in the dictionary and add other fields. Like, the type of pet, and other information that’s unique per pet. Speed, etc…

local pet_info = {
["RedPet"] = {
speed = 99;
}
}

Hope this helps :slight_smile:

I just realized if I access all the pets in the table with a for loop it would be a huge lag spike. is there a way to prevent the lag spike?

It would only spike if the player had up to 300 or more pets which may be possible in my game.

If you’re just doing a simple iteration over the player’s pets, I don’t think that would cause any lag; unless you’ve actually experienced this.

You can literally use :GetDescendants() on workspace and it usually returns within seconds and no lag, from my usage of it.

It depends on the amount you’re iterating through. If the number is high you can spike.

I’ve ran a code sample-

local d = {}

for i = 1,1000 do
	d["k"..i] = true
end

for i,v in pairs(d) do
	print(i,v)
end

And it compiled in seconds with no lag spike.

I don’t really think what you’d be doing is an issue at all.