Multiplace datastore saving works in Studio, but not in 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.

1 Like

I believe that you should save the player’s data every time they leave the game, rather than saving it when the server shuts down.
Here’s code that does 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.PlayerRemoving:Connect(Save)