How could I add a global pet serial system to my game?

Ok so I have a game with 3 purchasable exclusive pets. I want the 3rd and most expensive one to have a serial attached to its name.

If you dont know what I mean ill try and explain more. Games like pet simulator 99 or Other smaller but more popular simulators have a serial system that’ll say “#1” or “#7” depending on when the pet existed and how many exist. There were no youtube tutorials of how to do this but Id like to know asap. Any way of doing this? Let me know please :pray:

2 Likes

You could just save it in DataStore with the name of something like SerializedPets with the UserIDs that own the pets?
Maybe this will help?

I was thinking about this. I believe using UpdateAsync to claim an exclusive pet would work. You would create a key like “PetOreo#1” and value could be “na”. And then to claim in the update callback function, if it is not equal to “na” then dont change the value. If it is equal to “na” then change it to the player’s userId. Then after calling update, call GetAsync to check if it is equal to that player’s userId.

Additionally, you could use string manipulation (which im not an expert in) to use less key/values. Like key=“PetOreo” and value=“na/na/na/123455432/na/na”. Then to claim one your update callback function will look for an “na” spot and then if it finds it then replace the na with the userId. Again using GetAsync to check.

You would need to store a global datastore and increment it, something like:

local data = {...} -- load it in
local SerialHandler = {}

function SerialHandler.IncrementPet(petName: string)
    data[petName] = data[petName] or 0 -- Ensure it exists; avoid errors
    data[petName] += 1 -- Increment

    SerialHandler.Save()
end

function SerialHandler.Save()
    -- Do datastore magic and save.
end

return SerialHandler

If you want, you can also use MessagingService for real-time updates, you’ll just call it from IncrementPet, and have a listener in the file which would update the corresponding data but in the other server.


Just gonna comment on that, nice idea! Combine it with a compression algorithm and voila, you got a very compact string that stores all your data in a fraction of it’s size! Edited code would go like this:

local data = deserialize({...}) -- load it in and deserialize it.

--- Type representing serial data, to help with intellisense (code completion).
type SerialData = {
    [string]: string
}

--- Turns the string `str` into an actual data table, this isn't done once at
--- the start incase you use messaging service and things are expected to
--- update outside of `IncrementPet`.
local function load(str: string): SerialData
    -- example implementation, things are stored as `pet="[userid,...];"`

    local data: SerialData = {}
	
    for i, v in ipairs(string.split(str, ";")) do -- Each pet is split by `;`.
        local petName, owners = string.match(v, "(%w+)=%[(.*)%]") -- pretty simple pattern matching
        data[petName] = string.split(owners, ",") -- Get all owners (as strings!)
    end

    return data
end

local function save(data: SerialData): string
    -- example implementation, things are stored as `pet="[userid,...];"`

    local result = ""

    for i, v in pairs(data) do
        result ..= `{i}=[{table.concat(v, ",")}];`
    end

    return string.sub(result, 1, -2) -- remove trailing `;` at the end.
end

Compression would happen in save and decompression would be in load, note that this is an example and you may need to alter it (eg. be 100% sure they’re in the right order [they should])

1 Like

Wouldnt every server have different “data” for a shared key? If a player is only on one server at a time, the player’s data on that the server is therefore the correct updated data. But if multiple servers are accessing a key at the same time, you can’t rely on this. And with SetAsync, there is a possibility of collision.

I think your on the right track. But all the pets/datastore data are stored in a dictionary folders with all global data within the game. Within this folder theres a “PetPack” folder leading to folders called “1”, “2”, and “3”. And from the “3” folder is a folder woth the pet name and number values corresponding to the pet data. Is there anyway I could implement a serial system via data like I describe? It would just make it a lot easier

That’s why I recommended MessagingService.

Can you elaborate on the design you want? I don’t exactly get it, correct me if I’m wrong.
You have:

PetPack
   |----1
   |----2
   |----3
        |--- what here?

(Also read the edit I made to the reply above.)

Datastore UpdateAsync method would help here in my opinion, basically

--Reward player the exclusive pet
local PetSerial
DataStore:UpdateAsync(ExclusivePetName, function(PrevTotalPets)
   PetSerial = PrevTotalPets + 1
   return PetSerial 
end)

--Set the exclusive pet serial to PetSerial

You could make a GUID system for pets that save pet ID’s by generating GUID’s. Then, make a serial dictionary, [1] being the first pet, then its value being the GUID.

Apologies for not responding for quite some time. After further researching experimenting with these scripts, the serial system works. I appreciate your help and everyone else who replied as well :grin:

Your welcome, glad to hear it! Feel free to ask if you had any doubts :).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.