How would you add to an existing DataStore from another place in my game's universe?

Hello,

I want to add to an existing value from a different game. Basically, the main game pulls the KOs and WOs of the player to show them what they have, and the other game actually has KOs and WOs (which is where I’m confused). How would I add to a value from a different game, such as if the player has 57 KOs and 12 WOs, if they get 5 KOs and 2 WOs, it will now display 62 KOs and 14 WOs in the main-game. I’m open to DataStore2.

Datastore in the main place:

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder")
	stats.Name = "Stats"
	stats.Parent = plr
	
	local KOs = Instance.new("IntValue")
	KOs.Name = "KOs"
	KOs.Parent = stats
	KOs.Value = 0
	
	local WOs = Instance.new("IntValue")
	WOs.Name = "KOs"
	WOs.Parent = stats
	WOs.Value = 0
	
	local data
	local data2
	
	local yeah, nah = pcall(function()
		data = DS:GetAsync(plr.UserId.."-KOs")
		data2 = DS:GetAsync(plr.UserId.."-WOs")
	end)
	
	if yeah then
		KOs.Value = data
		WOs.Value = data2
	else
		print("Crap couldn't get the data RIP")
		warn(nah)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local yeah, nah = pcall(function()
		DS:SetAsync(plr.UserId.."-KOs",plr.stats.KOs.Value)
		DS:SetAsync(plr.UserId.."-WOs",plr.stats.WOs.Value)
	end)
	
	if yeah then
		print("Yeah data saved cool alright")
	else
		print("Why the data didn't save:")
		warn(nah)
	end
end)

Datastore in the other place:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetGlobalDataStore("KOsWOs")
game.Players.PlayerRemoving:Connect(function(plr)
	local yeah, nah = pcall(function()
		DS:SetAsync(plr.UserId.."-KOs",plr.leaderstats.KOs.Value)
		DS:SetAsync(plr.UserId.."-WOs",plr.leaderstats.WOs.Value)
	end)
-- kinda confused beyond here how to do this
end)

Thanks,
OldBo5.

3 Likes

I’m fairly sure that you can use DataStoreService for games in the same universe.
As for games that are in separate universes (not connected to each other), you would probably have to find an alternative way (e.g. external databases)

2 Likes

I am using the same universe, just want to know how I would add to an existing DataStore without overwriting the value, kind of like KOsinDS.Value = KOsinDS.Value + KOs.Value

The same way you usually would, unless I’m misinterpreting your question. As far as I know, the game won’t create an additional database field for a different game as that’d pretty much defeat the objective of the datastores.

1 Like

Kind of like KOsinDS.Value = KOsinDS.Value + KOs.Value is what I’m attempting to do.

1 Like

No matter from which game you access it in the universe, you will always get the same datastore with the same data.
To transfer data over to a other universe, you are required to use a external service which is not offered by roblox itself.

So as long as your game exists in the same universe you can use your normal :GetAsync() and :Get()

1 Like

By “game”, I meant “place”. I just want to know how I would add the amount of KOs from the game that actually should give the player the KOs to the DataStore without overwriting the data. Sorry for the confusion.

If I am interpreting your question correctly, I would recommend using UpdateAsync. Also, you have not provided us with a piece of code for us to possibly help you implement it.

1 Like

Check out the code sample on the developer hub for UpdateAsync.

Also be aware that bumping is against the forum etiquette. If people haven’t responded well, it’s likely due to the title or the content of your post. In this case, your query appears to have been answered, and this category is not intended to be a place for somebody to write code for you from scratch.

3 Likes

Just copy+paste. Boom.
Your data saves in the entire game, not just one place.

Also don’t bump, it’s against the rules.

2 Likes