Best way to script pet levels for datastore

Hello,

I have a basic understanding of datastores and have written all of my datastores following a tutorial on youtube, but I am only familiar with using tables in datastores from reading posts on here.

My question is what would be the best way to save a table of a player’s pet levels in order to save that table to a datastore?

Right now all my pets level up based on the player level, but each time the player levels up, I would like to give them one pet level so they can choose which pet to level up. (like skill points)

I think I should create an int value in the character model for each pet and another of unspent points. (also for resetting attribute points)
Then I should create a table or dictionary like:

local petlevel = { dog = 1,
                   cat = 1,
                   spider = 1
}

And then increase the int value as the pets get leveled up and then when I save, make the values in the dictionary what the int value is? Then how would I iterate over the dictionary to get the values back when a player joins?

I have been struggling with this for a few weeks now and need to make sure I’m on the right track before I give up.

Thanks.

i think, that you just need to save is as everything else

To iterate over all the key-value pairs in your dictionary you can use pairs like so:

for pet, level in pairs(petlevel) do
    print(pet, level) -- "dog 1, cat 1, spider 1"
end

I recommend moving the values to the player’s object rather than their character as the character resets with every respawn. To ensure more organization we can also put these values under a folder called “Data” in the player. We then add on to the code to set the values inside your player to their respective values. (Assuming that the values are named the same as the keys in the dictionary)

for pet, level in pairs(petlevel) do
    player.Data[pet].Value = level
end
2 Likes

Your idea and what @RetributionVI said are pretty correct and would work. But this is just my suggestion:
Instead of putting the pets levels an int object, and save what’s in the int objects once the player leaves, you don’t use the int object, and save the pets’ levels once a pet levels up, this is more simple and it will reduce data losses if they happen. And why using this idea is good, because technically the pets won’t be leveling up insanley fast which might result to many datastores requests, they level up every once in a while which is fine.

But if you want a simpler choice, go for what you said

2 Likes

Also note that for saving data it might be a good idea to use the pet names as numbers, too, to lessen the amount of space used (if this is a concern).

For example,

local petLevel = {[1] = 1, [2] = 1, [3] = 1}
-- [1] is 'dog', [2] is 'cat', [3] is 'spider'

This might be inconvenient for you to understand the data, though, so it is possible that you could have a table of pet IDs based on names.

local petIds = {dog = 1, cat = 2, spider = 3}
local petLevels = {[petIds.dog] = 1, [petIds.cat] = 1, [petIds.spider] = 1}

Don’t save the petIds table in the DataStore, as it is meant to be used only within the server and shouldn’t change for different players. The reason this is better for data saving is because numbers use less space than names and so therefore if you have a large amount of data for a player you don’t need to worry about exceeding space limits.

And of course, like what starmaq said, only save levels for pets that are NOT at the default level (1?). So if you don’t level up a pet, its level isn’t saved in the DataStore :wink:

5 Likes

Thanks for the replies.

I am going to take some time to consider all three options.
That’s exactly what I needed for the datastore Retribution.

starmaq I think that is a good idea too, but if I am making a way to refund pet level points, it is possible that they will be rapidly leveling up pets after a refund. Always a feature I could scrap too if I am having trouble with the int values.

Thanks Emerald, I didn’t know that about datastores. I was only aware of the limit to the amount of requests and not the total size.

Again, thanks all. I should have the revamped pet system up and running by the holidays since I have to do the bulk of building on weekends. Hope you all have happy holidays.