Hey there,
I am currently working on a game in which i will need to save upwards of 50 int values in a datastore, What would the most effective/lag reducing way of achiving this be??
Just save a table that has those 50 ints in it. Not sure what the problem is…
How would I be able to save 50 values with a corresponding name for each one?
Make a dictionary and set a key for each value:
local data = {
["Level"] = 6,
["Money"] = 7893,
["Cats"] = 90
}
Or you can have a simple table as long as you know what number has what value:
local data = {
6,
7893,
90
}
--And then when loading
local level = data[1]
local money = data[2]
local cats = data[3]
Thank you so much! Also this is a bit off topic but would you happen to know how to get a value out of the data store in a different script?
Well, you can get data from a datastore from any script, you can load it again in the second script I guess.
Ohhhhhhhhh alright, Thank you.
Hey sorry, I got one more question, If i were to store a value for the entire server, how would I save it and and get the same value in multi servers as it updates. For example if I were to have a server wide death counter, and when ever someone dies it were to change the data stores value by +1 and then in another server they get the data and it has that +1 death from the other server?
For between servers communication I would recommend checking out how the MemoryStore service works:
Introducing MemoryStore
On a concrete example, if you want to build a Leaderboard of deaths:
For a given server:
- If the leaderboard is not initialized in the MemoryStore, first initialize it using Datastores. (for example the top 100-1000 players) – This will only happen once for each server.
- When updating a player’s death counter Datastore, if the player is within those first 100-1000 players, update it in the MemoryStore as well. – This happens everytime someone dies.
- Do regular reads from time to time to see whether the MemoryStore data (that is cross-server) used for the leaderboards is different from your local server’s. If yes, it means some other server wrote new data to the MemoryStore, so update your leaderboard! – This happens every couple of seconds.
I read through the MemoryStore and im not quite sure how I would get a live update of a value in a data store, Ill just tell you what im actually trying to do, So I have a webhook that send a message to discord whenever a special pet is hatched in my game and it works fine and all but im trying to add a counter for how many times the pet has been hatched, But if in 1 server the value is lets say 4 and then it gets hatched and it changes to 5 how do i tell every other server that it has changed and is not 4 anymore and is now 5?
This is what I was trying to explain in my example above. You will need to change a little bit how your system works.
If every server knows how many times a pet has been hatched, then I suppose you want to have some system similar to a leaderboard, where you keep track of how many times each pet in your game has been hatched across all servers.
The most efficient way to do this is using the MemoryStore, since it is similar to a cache that is shared across servers.
Let me explain it in a simpler way.
If you wanted to implement this system without MemoryStore, what you would do is, if a certain pet has hatched, update the datastore by incrementing the corresponding value by 1.
Each server, from time to time would read this datastore, and would use this info to display certain info in your game (a leaderboard for example).
However, this is expensive, and can’t be done too often as you have a limit of reads!
That’s where the MemoryStore comes in handy. You can Read/Write to it a lot of times and it is very fast and cross-server, specifically designed for cases like this. You can read it every couple of seconds for example and update your leaderboard. However, IT IS NOT persistent. Meaning that if all your servers close, the data in the MemoryStore will be lost.
Try rereading the example I gave above and tell me if it’s clearer now.
Also, in the case that I might have misinterpretted something, please tell me, as it’s possible that I haven’t perfectly understood what you were looking for!
Thanks for clearing it up! But if its not persistent, How will I be able to save it? Cus my game isnt popular and will have 0 players at times.
When writing to the MemoryStore, also update in the DataStore. I explaind earlier above. You can change the concept from deaths to pets hatched and it will work:
Alright, thank you ill try and do this.
Im still lost on how I will actually tell each server how many times a pet has been hatched, and more importantly when it updates, I was looking around for some possible ways to do it and found the MessagingService and I was wondering if that would be able to do it.
True, the concept can be a little complex at first to get, but once you get the grasp of it it’s really simple.
You don’t tell each server how many times a pet has been hatched.
The Memory Store is a memory that is shared across all servers. Therefore, you want to maintain the hatch number data within it.
Each server will read it every 5 seconds, therefore each server will have the correct values.
Each server will also update values in the Memory Store based on its players hatching pets, so that other servers can read from it the new values.
The only thing I added above is that you will need to first initialize the Memory Store, with the values found within your DataStore.
If you still don’t understand this, I’d recommend you find out how a leaderboard works. There are plenty of tutorials for it, and the concept is really the same. It is then up to you if you want to use Memory Store or DataStores only.
Good luck!
Alright ill give it a shot, Thank you.