Hello! For my collectable system, I’m making sure the player will never get the same item again by assigning a GUID. The GUID generation works, but I was wondering how do I check for it? I’m using DataStores for saving. I had the thought of adding the GUID to a StringValue inside the Player, but I don’t know how to get it.
I’d say use DataStore2 because you can get the datastore from that with ease and it won’t error (kinda), it backs up data incase it doesn’t save and stuff like that, idk. Check using that everytime the player does the thing that generates the item.
Can I suggest something totally different?
I don’t think you need GUID at all. G/UUIDs are usually used to identify specific instances (records), not types, of things. They’re also usually reserved for database keys and things (but even then there’s usually better options).
Just assign each collectible type their own ID (no need to be a GUID, just literally a sequential ID will work – think roblox’s AssetIds – or it could even be a string) and save a list of those in your datastore. Your game can then pull that list for the player, and figure out which collectibles those IDs correspond to.
I’m not sure what you mean by
You would just compare the ID you get from your datastore to some map you keep in your game. Presumably you would need to know this to save the IDs in the first place.
local myCollectibles = { -- map from IDs to "something" (depends what your collectibles are)
1 = "bulbasaur",
2 = "ivysaur",
...
41 = "zubat"
}
local function GetCollectiblesForPlayer(player)
local listOfIdsFromDataStore = ... -- TODO
print("Player " .. player.Name .. " has the following collectibles:")
for _, id in pairs(listOfIdsFromDataStore) do
local collectible = myCollectibles[id]
if (collectible) then
print(" " .. id .. " -> " .. myCollectibles[id])
else
print(" " .. id .. " -> [ UNKNOWN ]")
end
end
end
I mostly chose GUIDs as a way for me to automatically be able to assign a number to make level designing faster, without having to manually add a ID to every collectable the player will ever get. Can I achieve that with your solution? Thank you for such a in-depth reply! And about “how do I check for it”, I wasn’t sure how to find it in a StringValue full of what the player collected, but I think string.match would work very well if I were to continue with my own system. Sorry if I didn’t understand anything correctly.
-
Can you explain this?
When you get a list of GUIDs from your datastore, how are you correlating those with objects? Are you just looping through all the collectibles in your place and seeing if any of the IDs match?
-
Also, can you expand on what you mean by “collectible”, and what exactly gets an ID? Are you talking about like, coins that are scattered throughout the world, and each coin gets a unique ID?
Or maybe you’re talking about achievements or something, where you only can have one of each achievement type?
First question:
I was using some code to find the GUID. This script is a child of the object. This destroys the object if found in a StringValue containing the GUID, which is what prompted me to start this thread
Second question:
I am placing certain types of coins around the level. There is no random generation, but I want to give the coins a unique ID for each entity placed, so I can make sure the player never sees the collected coin again until they erase their saved game.
Sorry about not being so specific
Checking it means storing all collected ones in a table for example.
Ahh, OK, that clarifies things.
So that’s not a terrible way to do things, but there are some pitfalls I see and some suggestions:
-
Make sure you’re generating these IDs at build-time, not run-time. They need to be the same each time you run the game, so they’ll need to be baked into the coin somehow. A StringValue in the coin with the ID would be fine, yeah. If UUIDs are convenient for you, use em. Just don’t re-generate when the game runs.
-
I would tag all the coins with CollectionService (which you might be doing anyways), which will let you easily get a list of all coins in the level. When a level loads, you can then have the client loop through and delete those coins that match one of the IDs in the list from the Datastore (which you’ll need to ask the server for). I think you can do this in O(number of coins) time.
-
It might also be useful to keep around a map from IDs to coin instances currently instantiated. You can create this at build time, or you could build it once when the game starts.
The GenGUID function keeps generating new ones, they don’t stay the same for the player/player’s ip.
If you are making a save-load function with this then good luck trying to get the player’s GUID back to check if the player has the first generated GUID.