Where is DataStore2 inefficient that your code “fixes”?
I must’ve misunderstood how datastore2 actually works, So it doesn’t fix anything, it just is a way of using datastore2.
The way I want to save and update a table is by using :GetTable() → modify table → Set()
Is it possible that when this is done by 2 scripts simultaneously, the data from one of the scripts won’t be saved because the other script overwrites the data? If so, are there any alternatives to save a table simultaneously without risking data-loss?
example picking up Gold and Silver from the ground at exactly the same time
script1: (gives gold)
GetTable() → tablename[gold] = tablename[gold] + 1
script2: (gives silver)
GetTable() → tablename[silver] = tablename[silver] + 1
script1:
:Set(tablename)
script2:
:Set(tablename)
does this example means that the data which script 1 is trying to update, is not being saved? since script2 is overwriting the data with his own data, which did not include the data script 1 was about to :Set() because script 2 used GetTable() before script1 Set() its data?
Roblox is single threaded, so you won’t have real multithreading issues. I’ve never had this issue and I regularly use several scripts handling data. You could have your own issues though if you have yields.
Take for instance, this pseudo code:
local currenciesStore = DataStore2("Currencies", player)
local currencies = currenciesStore:GetTable({ Gold = 0, Silver = 0 })
-- do some number crunching to determine currencies
currenciesStore:Set(currencies)
If our crunching doesn’t yield, then here’s the process:
- Get the data in the store
- Do some number crunching on it (like giving them free gold or something)
- Set the data
This is so far so good. But if our crunching yields, everything breaks!
- Get the data in the store
- Crunch numbers on it
- Yield, let other scripts have a chance to run
- Those other scripts might set their own data to the data store, but we don’t know it!
- We override the data with the old data!
Be wary and keep this in mind.
I think I’m missing some basic understanding about what single threaded means.
Doesn’t roblox run multiple threads at once? like multiple while loops? If so, why won’t script2 get a chance to run if script1 is not yielding? Is the interval between :get and :set just too small for script2 to use old data? Or does the thread “pause” whenever it is yielding giving script2 a chance to run it’s thread untill the yielding (pause) is over?
Since :Get() is called when a player is joining the game, it will not yield because script1 and script2 will be ran while the player is playing the game, and since Set() never yields, yielding won’t be a problem for me.
No. Roblox uses green threading, meaning that your code looks like it is using multiple threads but it’s actually just very intelligently swapping whenever you yield.
:Get() (after the first time) and :Set() never yield, so the threads aren’t switched while it’s still running.
Okay, I edited my previous post a bit too late I see,
I added the the question whether yielding “pauses” the thread, giving the other script a chance to run while it is paused. You gave me the answer anyway!
Thank you.
Can I use global data stores for this?
Not sure what you mean by this? Do you mean with non-player keys? If so, no.
If I save data in place1 then teleport the player into a branch-place can I still get the same saved data from place1?
Yes, DataStore2 works fine and keeps data between places of the same game.
I am planning on using this instead of the default datastore in my game, i am saving with tables.
if and how would this be possible? would i do it the same as if i was doing a normal datastore
tables are the only way i can save as my data needs large amounts of info such as level, xp, skins etc.
if i can’t do this, how would i do it?
Ok, so… i roughly understand how it’s supposed to work, but what i’m wondering is what does this imply? How does this affect throttling?
How do i go about setting the data? Do i still have to do ~ 3 min. intervals between saving, and if so, does it mean both of them, or just one?
My intuition says that i shouldn’t care about the regular data store causing trouble, if the keys are different, i’m not overwriting then, but i should have the ordered data store at intervals, is this true?
Would it still be a good idea to just require the model you have on ROBLOX? I’m only asking because I don’t see that in your post anymore and noticed it hasn’t been updated since August.
No, I don’t recommend this anymore. That model is outdated and Roblox Studio isn’t letting me update it.
Is it not possible to get player data for players that are not in the server? My use case is teleporting players to their friends who are in reserved servers using a reserve code stored in the friends datastore. But you can only retrieve datastores with player instances.
Something like…
local FriendPresenceStore = DataStore2("Presence", FriendUserId)
local Presence = FriendPresenceStore:Get({})
local PlaceId = Presence.PlaceId
local ReserveCode = Presence.ReserveCode
FriendPresenceStore:UncacheWithoutSaving() -- For example.
I’ve answered this several times in the thread, but no. This is planned, but no work has been done: A way to get data of an offline user · Issue #44 · Kampfkarren/Roblox · GitHub
this may be a silly question but, does the data save across places within games (for example will your “XP” value be the same in the Hub as Level1)
Yes, I’m using this in a multi-place game and it works fine. Just make sure to save the player’s data before teleporting them.