Recently, I’ve been invested in this universal story-based game, where once you teleport with a group of friends and play thru the game, you receive 1 point.
but since it’s 2 seperate games (all universal though) I have no idea how to do that.
Currently I have a datastore in the main lobby
Code Here
local Datastore = game:GetService("DataStoreService"):GetDataStore("Wins")
local players = game.Players:GetChildren()
game.Players.PlayerAdded:Connect(function(Plr)
local Key = Plr.UserId
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Plr
local Wins = Instance.new("IntValue",Leaderstats)
Wins.Name = "Wins"
local winsamount = Datastore:GetAsync(Key,Wins) or 0
Wins.Value = winsamount
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Key = plr.UserId
Datastore:SetAsync(Key,plr.leaderstats.Wins.Value)
end)
game.Close:Connect(function()
local players = game.Players:GetChildren()
for i = 1,#players do
Datastore:SetAsync(players[i].UserId,players[i].leaderstats.Wins.Value)
end
end)
which works, and I heard that if you use the same named datastore in the universal place, that it would save , so in the second game I have this:
local Datastore = game:GetService("DataStoreService"):GetDataStore("Wins")
-- Some code here
local playerss = game.Players:GetChildren()
for i = 1,#playerss do
Datastore:IncrementAsync(playerss[i].UserId, 1)
end
however, when me and my friend test this, when we arrive back in the lobby, our points have not gone up, would someone be kind enough to help a fellow dev out?
Quick question, wouldn’t you be able to make both of these games just a place under the same game? That would most likely solve your universal datastore problems.
So it looks like it should be possible to access data stores across places, you might just need to use the second scope argument to the :GetDataStore function? I don’t really know what is happening if your datastores are not retrieving the same data…
It’s just a theory, since I don’t know if scopes actually affect the accessibility of data.
Apparently, datastores already have a default scope if you don’t input one, that being "global":
Multiple places within the same game (“universe”) have access to the same data. I’m pretty sure “universe” is an outdated term that got replaced with “game” when this whole place/game thing started.
I mean, you could try using DataStoreService:GetGlobalDataStore to see if this makes any difference, it most likely won’t, since it returns the same kind of datastore that :GetDataStore does.
A different solution would be saving your data to an exterior database using HttpService. I recently looked at a thread that suggested this:
It looks like you’re overwriting the incremented save when the player leaves, since the code only calls IncrementAsync and does not increment the value that is saved when the player leaves.
Does IncrementAsync not save the value? I thought the whole point of that method was to increase a given datastore by the amount enlisted in the second parameter.
Also, I want to try and avoid having a leaderboard in the second place, so I couldnt set the value ( atleast I dont think)
I guess what @1waffle1 is suggesting is just to remove the :SetAsync from the game.Players.PlayerRemoving and game.Close events, allow the IncrementAsync function to take effect in the other place, and see if that affects the value returned from winsamount = Datastore:GetAsync(Key,Wins) or 0. I would suggest printing what Datastore:GetAsync returns before changing all this, just to see if it is returning 0 or nil. This could affect if the datastore is either saving the value and reseting it back to 0, or it is not saving the value in the first place, and :IncrementAsync is not saving the value in the datastore like waffle is implying. Correct me if you mean otherwise, 1waffle1!
By the way, :GetAsync only uses the first argument:
I tried that already, I just dont think a value is even coming through, I have a print now:
local Datastore = game:GetService("DataStoreService"):GetDataStore("Wins","GlobalWins")
local players = game.Players:GetChildren()
game.Players.PlayerAdded:Connect(function(Plr)
local Key = Plr.UserId
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Plr
local Wins = Instance.new("IntValue",Leaderstats)
Wins.Name = "Wins"
local winsamount = Datastore:GetAsync(Key,Wins)
print(winsamount)
Wins.Value = winsamount
end)
and in the other place i have
local Datastore = game:GetService(“DataStoreService”):GetDataStore(“Wins”)
wait(5)
local players = game.Players:GetChildren()
for i = 1,#players do
_G.Wins = Datastore:IncrementAsync(players[i].UserId,2)
print("New Wins:", _G.Wins)
wait(5)
end
winsamount always prints 0, but the second place always prints “New Wins:” and whatever the value is +2 every single time
You’re passing an object to the second parameter of GetAsync, which doesn’t make sense. It should probably error. If it doesn’t then it’s treating it as a scope and trying to read from the wrong place. actually GetAsync isn’t where scope is passed, it should just ignore your second parameter.
At the top of your first script, though, you are looking at a completely different datastore.