Suphi's DataStore Module

Yes im aware but for some reason it just will not load or it will take forever to load when its on. I dont know how to fix it. When i just tested it in the same game but in the dev version with all the same scripts etc it didn’t do it but when I do it in the main game with all the people it has issues.

Edit: Its when the servers soft shutdown and everyone rejoins it makes it so the data wont load for some reason.

(Now its spamming again and I dont know what to do)

1 Like

I was looking for new ways to save data since DataStore2 is really outdated nowadays and stumbled across this Suphi masterpiece.
Just like packet its lightweight, quick and flexible.
But unlike packet, this ones Syntax is rather complex and in my opinion: Too hard to use for games with lots of data and lots of data updates, which is fine because its still easier than the normal DataStoreService, but I’d rather its as simple to use as DataStore2, which lead me to make a super-simple addon for this that I’d like to share cus why not : )

“Config”

--- /// INSTALLATION AND USAGE (1-2 min) /// ---
-- 1. Parent this to Suphis DataStore Module. 
-- 1.1. Make sure this module has the "Loaded" attribute". Ensure its set to false.
-- 2. Look at and adjust the config below.
-- 2.1. Require this module instead of Suphis if you ever need to quickly modify some player data.
-- 3. Don't forget: This isn't a replacement for suphis module. It is an addon that allows quick changes.
-- 3.1. This module lacks very advanced features that Suphis module has.
-- 4. This module only contains 3 functions: Get, Set and Increment. Suggest more if you'd like to!

-- // Game Dependent Variables / "Config" // --
local dataStoreName = "" --> Sets the DataStore name
local playerDataTemplate = {} --> Sets the data template for new players. Example: local playerDataTemplate = {Cash = 10}
local handlePlayerData = true --> Sets whether or not this module sets up the opening / destroying of datastores for this game. Only Relevant if you wish to swap to this addon instead of using the module regularly
local compress = { --> Compression config to save data. Check out Suphis Forum post for details. This is the "safest" AND efficient way to compress.
	Level = 1,
	Decimals = 3,
	Safety = true
}

“Rest of the script”

-- // Imports / Globals // --
local __DataStores = script.Parent
local DataStores = require(__DataStores)
local Players = game:GetService("Players")
local Addon = {}

-- // Player Data Handling // --
if script:GetAttribute("Loaded") ~= true and handlePlayerData == true then

	Players.PlayerAdded:Connect(function(plr: Player)
		
		local DataStore = DataStores.new(dataStoreName, plr.UserId)
		DataStore.Metadata.Compress = compress
		
		local function attemptForceOpen()
			while DataStore.State == false do
				if DataStore:Open(playerDataTemplate) ~= DataStores.Response.Success then task.wait(5) end
			end
		end
		
		attemptForceOpen()
		
		DataStore.StateChanged:Connect(function(state)	
			attemptForceOpen()
		end)
		
	end)

	Players.PlayerRemoving:Connect(function(plr: Player)
		local DataStore = DataStores.find(dataStoreName, plr.UserId)
		if DataStore ~= nil then
			DataStore:Destroy()
		end
	end)
	
	script:SetAttribute("Loaded", true)

end

--[[Sets the value of key 'key' in the player's data store table to 'value']]
function Addon:Set(Player: Player, key: string?, value: any?)
	-- // Filter // --
	if not Player then error("Passed Player parameter is invalid") end
	if not Player:IsA("Player") then error("Passed Player parameter is not a player") end
	if not key then error("No data name / id provided") end
	
	local DataStore = DataStores.find(dataStoreName, Player.UserId)
	
	-- // 2nd Filter // --
	if not DataStore then error("DataStore not found") end
	if DataStore.State ~= true then error("DataStore is closed!") end
	
	DataStore.Value[key] = value
	
	if DataStore.Value[key] ~= value then
		warn("Value has not been set correctly") -- may be able to falsely fire if the value is updated frequently. disable these lines if so
	end
	
end

--[[Returns the value of key 'key' in the Players data table]]
function Addon:Get(Player: Player, key: string?)
	-- // Filter // --
	if not Player then error("Passed Player parameter is invalid") end
	if not Player:IsA("Player") then error("Passed Player parameter is not a player") end
	if not key then error("No data name / id provided") end
	
	local DataStore = DataStores.find(dataStoreName, Player.UserId)
	
	-- // 2nd Filter // --
	if not DataStore then error("DataStore not found") end
	if DataStore.State ~= true then error("DataStore is closed!") end
	
	return DataStore.Value[key]
	
end

--[[Increases numeric data table value of key 'key' by increment 'x']]
function Addon:Increment(Player: Player, key: string?, x: number?)
	-- // Filter // --
	if not Player then error("Passed Player parameter is invalid") end
	if not Player:IsA("Player") then error("Passed Player parameter is not a player") end
	if not key then error("No data name / id provided") end

	local DataStore = DataStores.find(dataStoreName, Player.UserId)

	-- // 2nd Filter // --
	if not DataStore then error("DataStore not found") end
	if DataStore.State ~= true then error("DataStore is closed!") end
	if typeof(DataStore.Value[key]) ~= "number" then error("Value of key '" .. key .. "' is '" .. typeof(DataStore.Value[key]) .. "', expected number") end

	DataStore.Value[key] += x
end

return Addon

Thanks suphi for making this incredible module
EasyPlayerDataAddon.lua (4.5 KB)

Edit!!: dont use this if youre planing on updating data alot using table.clone. it may or may not corrupt your data. I recommend just keeping the first part (Player DataHandling + waitfordata) and then write your own get function that returns the data store object rather than the value for a certain key.

yo suphi,

first of all i want to say that ive been using your module and its really well made. the structure makes managing data much easier.

i ran into a scenario where i wanted to fetch the dataStore of a player who isn’t currently in game, but it doesn’t seem to work.

is this something that was intentional for safety/performance reasons or is it just not implemented yet? im curious if there’s a recommended way to handle offline players without modifying the core module too much.

thanks in advance for your insight!

you can use datastore:Read() to load there data but be warned that if there online on another server the data you read might not be the latest

In the advance video I cover how to open a data store while there offline

thanks a lot i got it working using .hidden as explained in the video