Sometimes it saves, sometimes it does not

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SafeStorageData")
local HTTPService = game:GetService("HttpService")

Players.PlayerAdded:Connect(function(plr)
	local SafeStorageFolder = Instance.new("Folder")
	SafeStorageFolder.Parent = plr
	SafeStorageFolder.Name = "SafeStorage"
	
	local Data
	local success, err = pcall(function()
		Data = DataStore:GetAsync("Player_"..plr.UserId)
	end)
	
	if success then
		print("Successfully loaded!")
		if Data then
			local DataDecoded = HTTPService:JSONDecode(Data)
			print(DataDecoded)
		end
	else 
		print(err)
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local SafeStorageFolder = plr:FindFirstChild("SafeStorage")
	local SafeStorageFolderChildrens = SafeStorageFolder:GetChildren()
	local SafeStorageFolderEncoded = HTTPService:JSONEncode(SafeStorageFolderChildrens)
	
	
	local success, err = pcall(function()
		DataStore:SetAsync("Player_"..plr.UserId, SafeStorageFolderEncoded)
	end)
	if success then
		print("Successfully saved!")
	else 
		print(err)
	end
end)

My script sometimes saves the data and occasionally does not. I’m not sure why. I’ve encoded it because it’s says something like array error.

1 Like

Try using a bindtoclose function.

2 Likes

Try using the BindToClose function like @dmksa123 has said, I’ve added it with your code, so try something like this:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SafeStorageData")
local HTTPService = game:GetService("HttpService")

Players.PlayerAdded:Connect(function(plr)
	local SafeStorageFolder = Instance.new("Folder")
	SafeStorageFolder.Parent = plr
	SafeStorageFolder.Name = "SafeStorage"
	
	local Data
	local success, err = pcall(function()
		Data = DataStore:GetAsync("Player_"..plr.UserId)
	end)
	
	if success then
		print("Successfully loaded!")
		if Data then
			local DataDecoded = HTTPService:JSONDecode(Data)
			print(DataDecoded)
		end
	else 
		print(err)
	end
end)

Players.PlayerRemoving:Connect(function(plr)
	local SafeStorageFolder = plr:FindFirstChild("SafeStorage")
	local SafeStorageFolderChildrens = SafeStorageFolder:GetChildren()
	local SafeStorageFolderEncoded = HTTPService:JSONEncode(SafeStorageFolderChildrens)
	
	
	local success, err = pcall(function()
		DataStore:SetAsync("Player_"..plr.UserId, SafeStorageFolderEncoded)
	end)
	if success then
		print("Successfully saved!")
	else 
		print(err)
	end
end)

game:BindToClose(function()
for _, plr in ipairs(game.Players:GetPlayers()) do
local SafeStorageFolder = plr:FindFirstChild("SafeStorage")
	local SafeStorageFolderChildrens = SafeStorageFolder:GetChildren()
	local SafeStorageFolderEncoded = HTTPService:JSONEncode(SafeStorageFolderChildrens)
	
	
	local success, err = pcall(function()
		DataStore:SetAsync("Player_"..plr.UserId, SafeStorageFolderEncoded)
	end)
	if success then
		print("Successfully saved!")
	else 
		print(err)
	end
end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.