Multiplace datastore works in studio but not in roblox player

Sorry for the bad title, I dont know how to make it less confusing

What do you want to achieve?
I want to make a datastore that is accessible through multiplie places within the same game/universe and when I change a leaderstats value it gets saved and is accessible through the main place.

What is the issue?
The code I made seems to work fine in the studio (when I press stop it saves and is accessible in the main place) but when I try to save it in the Roblox Player it doesnt save and isnt accessible in the main place.

What solutions have you tried so far?
Tried to look on the Developer hub but nothing regarding my issue.

Here is the datastore server script, placed in ServerScriptStorage, in both the main place and the second place (Ill call the main place “place A” and the other “place B”)

local datastoreservice = game:GetService("DataStoreService")
local datastorage = datastoreservice:GetDataStore("datastorage")
print("Variables loaded")

local currentSession = {}

local function Load(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Nights = Instance.new("IntValue")
	Nights.Name = "Nights"
	Nights.Parent = leaderstats
	
	print("Leaderstats created")
	
	local data
	local success, errormessage = pcall(function()
		data = datastorage:GetAsync(player.UserId)
	end)
	print("Data load happened")
	
	if success then 
		print("Data loaded") 
		if data then
			player.leaderstats.Nights.Value = data
			print(Nights.Value)
		end
	else
		warn("Error happened while loading data: "..errormessage)
	end
end

local function Save(player)
	for i, player in pairs(game:GetService("Players"):GetPlayers()) do
		local success, errormessage = pcall(function()
			datastorage:SetAsync(player.UserId, player.leaderstats.Nights.Value)        
		end)    
		
		if success then
			print("Data saved")
		else
			warn("Error happened while saving data: "..errormessage)
		end     
	end
end

game.Players.PlayerAdded:Connect(Load)
game:BindToClose(Save)

In “place B”, when I change the Nights value with a click detector, it saves when im in Roblox Studio (change the value in place B, stop play testing in place B, start play testing in place A and print the value, which works), but it doesnt work when I play in Roblox Player.

Additional information: I close the Roblox Studio by pressing the X/Cross sign on top of the program.

In the game:BindToClose() you should use a for loop.

1 Like

Like this:

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers) do
		Save(v)
	end
end)
1 Like

this doesnt work, works in studio but not in roblox player

Have you turned on data store service in game settings?

game:BindToClose only fires when the server is shutting down. If multiple people are in your server and one of them leaves, their data won’t be saved. Instead, use game.Players.PlayerRemoving and save the data of the player that’s leaving.

local function Save(player)
     local success, errormessage = pcall(function()
         datastorage:SetAsync(player.UserId, player.leaderstats.Nights.Value)
     end)

     if success then
	     print("Data saved")
     else
	     warn("Error happened while saving data: "..errormessage)
	 end 
end

local function SaveAll()
     if not game.RunService:IsStudio() then
          for _, player in pairs(game.Players:GetPlayers()) do
              Save(player)
          end
     end
end

game.Players.PlayerRemoving:Connect(Save)
game:BindToClose(SaveAll)
1 Like

Ok so the issue is when you are in studio and press stop it fires the bindtoclose function while your player is still in the game the save function will then go through the players and save their data. while in a real server unless you specifically shut the server down data will never save because the only time the save function will fire is when all players have left the game, so when it does fire the save function there are no players to iterate through. I would recommend doing this.

local datastoreservice = game:GetService("DataStoreService")
local datastorage = datastoreservice:GetDataStore("datastorage")
print("Variables loaded")

local currentSession = {}

local function Load(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Nights = Instance.new("IntValue")
	Nights.Name = "Nights"
	Nights.Parent = leaderstats
	
	print("Leaderstats created")
	
	local data
	local success, errormessage = pcall(function()
		data = datastorage:GetAsync(player.UserId)
	end)
	print("Data load happened")
	
	if success then 
		print("Data loaded") 
		if data then
			player.leaderstats.Nights.Value = data
			print(Nights.Value)
		end
	else
		warn("Error happened while loading data: "..errormessage)
	end
end

local function Save(player)
	local success, errormessage = pcall(function()
		datastorage:SetAsync(player.UserId, player.leaderstats.Nights.Value)        
	end)    
		
	if success then
		print("Data saved")
	else
		warn("Error happened while saving data: "..errormessage)
	end     
end

game.Players.PlayerAdded:Connect(Load)
game.Players.PlayerRemoved:Connect(Save)
game:BindToClose(function()
	for i,v in pairs(game.Players:GetChildren()) do
		Save(v)
	end
end)
1 Like

yes i have, i have studio api turned on in both places

Thank you, it fixed my problem

Thank you for your help. This seems to work aswell as the other solution.