DataStores breaking whenever game is ran on Roblox rather than Studio

Intended Function
I want my saving script to save a set of values for each player in a datastore and for that information to remain uncorrupted. It’s a Multi-Dimensional table and a sample of some data that is usually store in it is below:


Issue
Whenever I play the game normally in studio everything works as expected and the data is stored and retrieved properly. When I test the game on Roblox the data store is corrupted and instead of the normal array that should be returned I get:
Screenshot 2024-03-16 141729
This issue makes no sense to me as it works perfectly fine on studio but who knows?
Attempted fixes/Context
There’s not really a lot for me to try. Other datastores work on Roblox and Studio as normal and they work in between places. I have looked in to the data being stored and it is all either a string, boolean, int, or number. I am not saving any instances or anything weird like that to my knowledge. This script has worked perfectly fine in other games so I don’t know what is causing this.
Scripts
I am willing so send any context that you would need in order to be able to help diagnose this issue if requested.

1 Like

Are you using a BindToClose() function at all?

I was having this exact same problem. Studio was updated to shutdown the server much much quicker in a playtest, not allowing time for process’ to run- like datastores.

Put this somewhere in the script that saves the datastore, it just makes studio wait 3 seconds when you stop playtesting

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		task.wait(3)
	end
end)

The issue is that it doesn’t save on roblox servers, but does save in studio.

game:BindToClose(function()
    if game:GetService("RunService"):IsStudio() then task.wait(3) return nil end
    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        savingFunctionHere(player)
    end
    task.wait(3)
end)
1 Like

I probably should have clarified this but the issue isn’t saving but rather it loads corrupted somehow and then it saves it as corrupted. The load issue thing never occurs in studio though.

function module:load(key)
	cachedata[key] = store:GetAsync(key)
    hasBeenLoaded[key] = true
	print(cachedata[key])
	if cachedata[key] == nil then
		cachedata[key] = {}
	end
end

This is my loading code for reference if it helps key is the player’s userid. I have made sure that it first loads then saves the code. It loads completely fine in studio but when it loads in the game I assume it gets that weird corrupted table.

I need all the code for the datastore because this code is not sufficient for me to see the problem

1 Like

Sorry for my late response but here:

local module = {}
local data1 = game:GetService("DataStoreService")
local store = data1:GetDataStore("PlayerData")
local cachedata = {}
local hasBeenLoaded = {}

function module:save(db, key, data)
	if type(key) == "string" then
		if cachedata[key] == nil then
			self:load(key)
			wait(1)
			spawn(function()
				while true do
					module:update(key)
					wait(10)
				end
			end)
		end
		cachedata[key] = data
	else
		if cachedata[key] == nil then
		repeat
			wait(1)
			until cachedata[key] ~= nil
		end
		if cachedata[key][db] == nil then
			cachedata[key]["DB"][#(cachedata[key]["DB"])+1] = db
		end
		cachedata[key][db] = data
	end
end
function module:load(key)
	cachedata[key] = store:GetAsync(key)
	hasBeenLoaded[key] = true
	print(cachedata[key])
	if cachedata[key] == nil then
		cachedata[key] = {}
	end
end
function module:read(db, key)
	if type(key) == "string" then
		if cachedata[key] == nil then
			self:load(key)
			wait(1)
			spawn(function()
				while true do
					module:update(key)
					wait(10)
				end
			end)
		end
		if cachedata[key] == nil then
			cachedata[key] = {}
		end
		return cachedata[key]
	else
		if cachedata[key] == nil then
			repeat
				wait(1)
			until cachedata[key] ~= nil
		end
		if cachedata[key][db] == nil then
			if cachedata[key]["DB"] == nil then
				cachedata[key]["DB"] = {}
			end
		end
		return cachedata[key][db]
	end
end
function module:update(key)
	local success, err = pcall(function()
		if hasBeenLoaded[key] then
			store:SetAsync(key, cachedata[key])
		end
	end)
end
return module

For refrence update is automatically called from the client every 10 seconds and is called when a player leaves or server shuts down according to key. Load should run only once and usually works right.

why did u use while true do,Not necessary,u only need to do a few attempts,(That can be the cause of the datastore to break)(Plus: what did u want to save)

Additionally(why is there a type(key)) show me that code for type(key)

Beacsue the while true do will be constantly running,and u didnt have any to break the function.Thats why datastore breaks as it has to still run the while true do function when the player leave

The reason why they have a few attempts is because it wont break the datastore.Whereas if u constantly running it ,it will cause the datastore to be exhausted and cause it to break.

Another place used a datastore called PlayerData which caused an override and that place would only be used if the game was played in roblox which was the cause of the bug. Weird how it’d never save though but yea it was all my fault.

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