Universal Datastore?

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? :open_mouth:

3 Likes

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.

2 Likes

By that , do you mean this?

hey

EDIT:

Oh do you mean just one entire game?

1 Like

That seems about correct, oh boy…

And yes, I do mean just one entire game, none of this universe stuff.

Anyway, I was researching your topic on the dev wiki. Naturally, this page came up on data stores:


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…

1 Like

So basically instead of the datastore I had before, have them both connect to one with the same scope?

ex:
local Datastore = game:GetService(“DataStoreService”):GetDataStore(“Wins”,“GlobalWins”)

1 Like

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":


So, I don’t really know if it makes a difference.

1 Like

Im testing it currently, i’ll be able to let you know if changing the scope works or not

2 Likes

And no still no luck, so I may need to resort to some other method of transfering data

2 Likes

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.

3 Likes

That’s what we are trying to figure out currently - why :IncrementAsync() isnt working in place #2 to increase the points (wins in this case) by 1

1 Like

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:

2 Likes

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.

1 Like

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)

2 Likes

You’re saving a different value when the player leaves. The value IncrementAsync changes to you are changing back.

2 Likes

How would I avoid reseting it every single time?

Currently I’m testing it out, and it seems the the place #2 continuously grows, however when you are brought back to the main place its 0.

It saves on the secondary place

1 Like

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:


So you could just change that to Datastore:GetAsync(Key). It doesn’t fix anything, just good to know.

2 Likes

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

3 Likes

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.

3 Likes