Is my datastore safe?

  1. What do you want to achieve? Keep it simple and clear!

So im trying to recreate personal servers. And i got this datastore which saves models and game settings

  1. What is the issue? Include screenshots / videos if possible!

It works. But im a bit scared to release the game before i know if its safe

This is the two pices of code which saves & loads data

GETTING DATA:

local DATA = nil
	local TRIES = 0
	local SUCESS, ERR = nil
	
	
	repeat
		SUCESS, ERR = pcall(function()
			DATA = GAME_INFO:GetAsync(KEY)
		end)
		TRIES = TRIES + 1
		task.wait(.1)
	until SUCESS == true or TRIES >= 5
	

	if SUCESS == true then
		
		if DATA ~= nil then
			warn("LOADED GAMES DATA")
			return DATA
		else
			warn("GAME HAS NO DATA. RETURNED NIL")
			return nil
		end
		
	else
		error("SOMETHING WENT WRONG GETTING GAMES DATA " ..ERR)
		return nil
	end
	
end

SAVING

local SUCESS, ERR = nil
	local VALS = game.ServerStorage.SERVER_INFO
	local ADMINS = game.ServerStorage.ADMINS
	local IS_LOCKED = VALS.IS_LOCKED
	local TOOLS_ENABLED = VALS.GIVE_ENABLED
	
	
	SUCESS, ERR = pcall(function()
		GAME_INFO:UpdateAsync(KEY, function(OLD)
			local NEW_LIST = game.HttpService:JSONDecode(OLD)
			local SERVER_TABLE = NEW_LIST[NAME] -- // NAME IS THE NAME OF THE DISCTIONARY? WE WANT TO GET
			
			
			if IS_LOCKED.Value ~= SERVER_TABLE["IS_LOCKED"] then
				SERVER_TABLE["IS_LOCKED"] = IS_LOCKED.Value
			end
			if TOOLS_ENABLED.Value ~= SERVER_TABLE["TOOLS_ENABLED"] then
				SERVER_TABLE["TOOLS_ENABLED"] = TOOLS_ENABLED.Value
			end
			
			if #ADMINS:GetChildren() ~= SERVER_TABLE["ADMINS"] then
				for i, v in pairs(ADMINS:GetChildren()) do
					table.insert(SERVER_TABLE["ADMINS"], v.Name)
				    end
				end
			
			if #game.Workspace.BLOCKS:GetChildren() <= 4000000 then
				
				if game.Workspace.BLOCKS:GetChildren() ~= SERVER_TABLE["OBJECTS"] then
					
					for i, v in pairs(game.Workspace.BLOCKS:GetChildren()) do
						SERVER_TABLE.OBJECTS[v.Name] = {
							["CF"] = {v.PrimaryPart.CFrame:GetComponents()};	
							["ORIEN"] = {v.PrimaryPart.CFrame:ToOrientation()}
						}
					end
				end
			else
				return nil
			end
			
			return game.HttpService:JSONEncode(NEW_LIST)
		end)
	end)
	
	
	if SUCESS == true then
		warn("GAMES INFO UPDATED!")
	else
		error("SOMETHING WENT WRONG TRYING TO SAVE GAMES INFO " ..ERR)
	end

First thing’s first, this should go into #code-review

Second thing, it mostly looks good, however there are a couple things that I would change:

First, CFrames contain rotational data so you don’t have to define it separately,

SERVER_TABLE.OBJECTS[v.Name] = {
    ["CF"] = {v.PrimaryPart.CFrame:GetComponents()};
}

Second thing, you don’t have to JSONEncode the data as it’s done when saving automatically.

    return NEW_LIST

Last thing, you should be checking the length of the data, not how many entries.

local encodedList = game:GetService('HttpService'):JSONEncode(NEW_LIST)
if encodedList:len() <= 4000000 then
    warn('Can be saved!')
end
1 Like

Alright. I’ll make sure to keep that in mind next time.

Also just a question. I already tried to just save the dictionary but then i got hit with an error. So encoding it was my only option?

This usually means that there is invalid data inside the dictionary, so check that first.

1 Like

Oh weird, you can probably just do the length of the data that’s encoded then, and return the encoded table,

local encodedList = game:GetService('HttpService'):JSONEncode(NEW_LIST)
if encodedList:len() <= 4000000 then
    warn('Can be saved!')
end
return encodedList

Although, you would probably have to encode it twice to take into account the extra JSONEncode call that takes place when your data is to be saved,

local encodedList = game:GetService('HttpService'):JSONEncode(NEW_LIST)
local encodedListToCheckLength = game:GetService('HTTPService'):JSONEncode(encodedList)
if encodedListToCheckLength:len() <= 4000000 then
    warn('Can be saved!')
end
return encodedList
1 Like

Oh yeah really quick sorry. Forgot to ask?

What do you mean with invalid data?. Like data thats nil or