How to make global datastores across games in asset manager

  1. I would like to access datastores across games in asset manager the data store is for wins i want to add wins to a player from a game in asset manager then i go to the lobby i want it to save them for a leaderboard.

  2. I don’t know how to access datastores across games in asset manager.

Screenshot 2024-04-14 213008
3. I have tried to script one for myself but i can’t do it.

This is the script if you want to edit it or make a new one. Thanks!

local DataStoreService = game:GetService(“DataStoreService”)
local Players = game:GetService(“Players”)

local winsDataStore = DataStoreService:GetDataStore(“WinsDataStore”)

local function savePlayerWins(player)
local userId = tostring(player.UserId)
local wins = player.leaderstats.Wins.Value

local success, errorMessage = pcall(function()
    winsDataStore:SetAsync(userId, wins)
end)

if not success then
    warn("Failed to save wins for player " .. player.Name .. ". Error: " .. errorMessage)
end

end

local function loadPlayerWins(player)
local userId = tostring(player.UserId)

local success, wins = pcall(function()
    return winsDataStore:GetAsync(userId)
end)

if success then
    player.leaderstats.Wins.Value = wins
else
    player.leaderstats.Wins.Value = 0
    warn("Failed to load wins for player " .. player.Name)
end

end

local function setupLeaderstats(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats

loadPlayerWins(player)

end

Players.PlayerAdded:Connect(function(player)
setupLeaderstats(player)

player.OnDestroy:Connect(function()
    savePlayerWins(player)
end)

end)

for _, player in ipairs(Players:GetPlayers()) do
setupLeaderstats(player)

player.OnDestroy:Connect(function()
    savePlayerWins(player)
end)

end

2 Likes

what is the event player.OnDestroy() ?

1 Like

i had it for when the player leaves

1 Like

can’t you just use the plugin “DataStore Editor”

1 Like

DataStore editor is only for editing the datastore values

Well from what i just read it states that you wanted to add wins to a player from a game, you can literally just use datastore editor and edit wins for a specific player if they have data in that database for wins

No, what i mean is i want a script to add wins to a player that wins the game then when they join back in the lobby it will show in the leaderboard how much wins they got.

just use a ordered data store bro

Will that work when i want to add wins to a player from a game in asset manager?

Although it is possible through Roblox, it’s one hell of a walk to get it fully working. I would recommend using BloxBiz Plugin.

The plugin allows you to create a datastore through a website, and then loads the data through HttpServices.

Consider researching more into the software, all you have to do once setup is duplicate your main script into the games you want to share the data with.

Hello,

When you mentioned “games in asset manager,” did you mean places within the same experience? If that’s the case, you can access the DataStore from all places within the experience just like you would with any other place. It works the same way and they are all shared.

It might also be a good idea to consider implementing session-locking to avoid any potential data loss when doing the stuff you mentioned.

I also suggest changing this to Player.PlayerRemoving.

Yes, i did mean places within the same experience. like this
image

But how would i do it that’s what i get lost on not sure if you can make me a script for the lobby and the game i would love if you could do that for me. Thanks

Sadly I don’t have time to write such a script.

You could use an solution such as ProfileService which handles session locking data for you.

Oh ok i will give it a try thanks

Feel free to reply to me if you have any issues, if the solution works for you, don’t forget to mark it as an solution <3.

Just before i go and try will this work for the data store and do i add on into the lobby and one in the main game?

local DataStoreService = game:GetService(“DataStoreService”)
local WinsDataStore = DataStoreService:GetDataStore(“Wins”)

local Players = game:GetService(“Players”)

local function updateWins(player, newWins)
local userId = tostring(player.UserId)

local success, errorMessage = pcall(function()
	WinsDataStore:SetAsync(userId, newWins)
end)

if not success then
	warn("Failed to update wins for player " .. player.Name .. ". Error: " .. errorMessage)
end

end

local function onSavePlayerWins(player)
local userId = tostring(player.UserId)
local wins = player.leaderstats.Wins.Value

local success, errorMessage = pcall(function()
	WinsDataStore:SetAsync(userId, wins)
end)

if not success then
	warn("Failed to save wins for player " .. player.Name .. ". Error: " .. errorMessage)
end

end

local function onLoadPlayerWins(player)
local userId = tostring(player.UserId)
local success, wins = pcall(function()
return WinsDataStore:GetAsync(userId)
end)

if success then
	if wins then
		player.leaderstats.Wins.Value = wins
	else
		player.leaderstats.Wins.Value = 0
	end
else
	warn("Failed to load wins for player " .. player.Name)
	player.leaderstats.Wins.Value = 0
end

end

local function onPlayerAdded(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats

onLoadPlayerWins(player)

end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end

Players.PlayerRemoving:Connect(onSavePlayerWins)

Please format the code correctly next time (use the preview feature!!), well, one way to find if it will work is for you to try it and see :). That’s the best way to see if it will work.

As for the second question, yeah you have to add the data script in both places obviously.

local DataStoreService = game:GetService("DataStoreService")
local WinsDataStore = DataStoreService:GetDataStore("Wins")

local Players = game:GetService("Players")

local function updateWins(player, newWins)
	local userId = tostring(player.UserId)

	local success, errorMessage = pcall(function()
		WinsDataStore:SetAsync(userId, newWins)
	end)

	if not success then
		warn("Failed to update wins for player " .. player.Name .. ". Error: " .. errorMessage)
	end
end

local function onSavePlayerWins(player)
	local userId = tostring(player.UserId)
	local wins = player.leaderstats.Wins.Value

	local success, errorMessage = pcall(function()
		WinsDataStore:SetAsync(userId, wins)
	end)

	if not success then
		warn("Failed to save wins for player " .. player.Name .. ". Error: " .. errorMessage)
	end
end

local function onLoadPlayerWins(player)
	local userId = tostring(player.UserId)
	local success, wins = pcall(function()
		return WinsDataStore:GetAsync(userId)
	end)

	if success then
		if wins then
			player.leaderstats.Wins.Value = wins
		else
			player.leaderstats.Wins.Value = 0
		end
	else
		warn("Failed to load wins for player " .. player.Name)
		player.leaderstats.Wins.Value = 0
	end
end

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	onLoadPlayerWins(player)

end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end

Players.PlayerRemoving:Connect(onSavePlayerWins)