I am making a game based around a global score, however, I am not sure how to make the score global. I will have an IntValue in the workspace called “GlobalCounter” which I want the value of which to be synced across all servers. I couldn’t find anything other than countdowns so I would appreciate instructions on how to do this or a video which explains it. The counter will not change on it’s own, but by player interaction. Thank you!
in order to communicate between servers we have a few options
Here is a example using the datastore
local DataStoreService = game:GetService("DataStoreService")
local scoreStore = DataStoreService:GetDataStore("Score")
local score = 0
local function UpdateAsync(savedScore)
if savedScore >= score then -- if the savedScore is higher
score = savedScore -- then update this server score value
else -- if the savedScore is lower then this servers score
return score -- then update the savedScore to match
end
end
local function SyncScore()
local success, value = pcall(scoreStore.UpdateAsync, scoreStore, "Key", UpdateAsync)
print(success, value)
-- if success = false then value = error message
-- if success = true and the saved score was updated then value is the same as score
-- if success = true and the servers score updated then value will be nil
end
while true do
task.wait(10) -- wait some time so we done use all are datastore limits
SyncScore() -- score value will synced with other server every 10 seconds
end
So with this, what value should the player interact with to change the score and how could they interact with it?
Does anyone have any other ideas?
Instead of a DataStore
, use a MemoryStore
instead.
Memory stores are designed to be updated often and are made for things like this.
local mss = game:GetService("MemoryStoreService")
local store = mss:GetHashMap("GlobalValue")
local function update(newVal: number)
--no UpdateAsync here because it's just a value that doesn't really matter as such
local success, result = pcall(store.SetAsync, store, "GlobalScore", newVal)
if not success then --[[error handle]] end
end
So how could this be implemented? How could I link this up with a value of which all players can change at anytime? How do I call on the value in other scripts?
Just detect when it’s changed.
local val = --[[path to your value]]
--debounce so we don't update too often and overload the store
local db = false
local function update()
if db then return nil end
db = true
--update the value
local success, result = pcall(store.SetAsync, store, "GlobalScore", newVal, --[[you can add an expire time here]])
task.wait(5)
db = false
end
local function get()
local success, result = pcall(store.GetAsync, store, "GlobalScore")
print(if success then result else "failure")
val.Value = if success then (result or 0) else (val.Value or 0)
end
val:GetPropertyChangedSignal("Value"):Connect(update)
and other scripts can just read val.Value
No, that’s bad practice. You should just save it when the player leaves or the server shuts down. And just to be sure, you can save it in an interval of a couple minutes.
You do understand this is for a global counter, so it needs to save as often as possible… This is not a regular datastore
Memory stores are designed to update regularly. It’s a count system, which essentially means there’s little to no point making it if it only saves when the player leaves. It’s bad practice for when it’s actual data, but this data is much less important.
The main reason it would be bad practice is eating through the data request budget and overwriting new data with old data. In a standard data store system, you would be correct, and other tactics like session locking and data versioning would be in place. The only applicable one here would be data budget, but memory stores use a different request budget to data stores.
Obviously you would need to keep a variable in your script which you update as often as possible, but you don’t need to do the same with the datastore.
The way I understood this, he wants the score to persist between sessions, which memory stores don’t do.
All “persisting between sessions” means is that once all game servers close, the data is lost. As long as at least one game server is up, the data will not be lost (provided it hasn’t hit the expiration time limit, which is 45 days after the last update). If the OP would like to keep data after shutting down servers, then it should be written to a data store only when the game closes and retrieved when the game starts back up again. Memory stores should be used when the value updates and data stores should only be used when the game closes, otherwise that would cause serious issues with the data request budget. The OP didn’t specify this, so I assumed not.
Idk I just want a score that players can contribute to… if you think you have a better method to it I would love to hear it
TheDevKing has a pretty good vid abt it: https://www.youtube.com/watch?v=sXpuGnVzsxw&t=197s. U can make this in StarterGui instead of making it a billboard if thats what u wanted.
I am kinda trash at scripting and idk how to apply this to my scenario. Idk how to use this to make a global score instead of leaderboard. The global score is like everyone is working on one stat and thats what idk how to make ig
This is what I meant with bad practice. But I did also misunderstand what exactly OP was looking to do. In that case he would need both Memory- and DataStores like you already suggested.
You can’t expect Roblox or the players to keep a server up at all times.
To simplify this problem you could really only either make the server really big so that so that you have enough spots for all your active players at a time.
Or you could update your DataStore every minute or so from a server value that collects all player values in that server.
If you make a nice animation to cover up that you’re adding more than just one, it shouldn’t really make a difference.
I’m thinking something like numbers scrolling up.
Best regards,
Pinker
A bit misleading… bad practice typically refers to “incorrect” use of data stores, like updating every time value is changed, not waiting for data to save when the game closes, etc., which does not apply to memory stores. Thanks for clarifying.
I hope you mean MemoryStore here… updating a data store every minute would be very bad practice.
sry mb. not really sure what u mean by “score”. Is it like some in game value or…
This is way more complicated than I thought… I was making this as a quick and easy game but I am not sure anymore
Yes and I meant of memory stores, since you have to rely on Roblox and players to keep your sever up so you don’t lose the score.
No, I only suggested this for the sake of simplicity, since OP said he wasn’t that good at scripting. Maybe it’s not best practice, but at least you’re not losing data.
Plus, I was talking about once a minute for every server.
PS: Nothing is ever easy with Roblox scripting.
Best regards,
Pinker