How to use Suphi's datastore module

Background

I am currently having trouble with Suphi’s datastore module. I tried watching a youtube video and I dont know what I did wrong. I feel as though potentially there may be something wrong with the module so I tried reading through the module itself however they deleted all the spaces so I was unable to do so. I hope that you guys can help because I really am lost.

Issue

I am trying to use this datastore module called suphis Datastore however its not working for some reason and there are no errors. My goal is to be able to retrieve data. To note this is my ONLY script for it. if i was supposed to configure something else let me know.


I defined my variables. As you can see I require the datastore module and I make a data template

local template = {hi = 0}
local DataStoreModule = require(script["Main Module"])
local dataStore = DataStoreModule.new("Player", userId)
local value = 1000
local key = "hi"

Then I add player added and removed events:

game.Players.PlayerAdded:Connect(function(plr))
	local userid = plr.UserId
	local myDataStore= DataStoreModule.new("Player", userid)
	myDataStore.StateChanged:Connect(StateChanged)
	StateChanged(dataStore.State, myDataStore)
end)
	
game.Players.PlayerRemoving:Connect(function(plr))
	local userId = plr.userId
	local myDataStore= DataStoreModule.find("Player", userid)
	if myDataStore~= nil then 
		myDataStore:Destroy() 
	end 
end)

and finally I try to open the data and retrieve the data:

if dataStore:Open() ~= "Success" then
	error("issue opening")
else
	dataStore.Value[key] = value -- set value
	print(dataStore.Value[key]) -- get value

	dataStore:Destroy()
end

So why isnt it working? Also to clarify the module is inside a script in workspace.

You really shouldn’t make a whole devforum post about a respected datastore module because of an “issue” you’re having. You haven’t confirmed that it’s the module and not just you. Even if it was the modules fault, that doesn’t mean the module sucks.

And just because the module doesn’t use spaces doesn’t mean it’s written badly. I haven’t looked into the module yet, though.

5 Likes

I didnt make the post to complain about the module, I made a post because I truly am lost about using it. I would really appreciate any help.

1 Like

do you think that since the module script is located in workspace it wont work?

It doesn’t matter where it is, as long if it’s accessible and it returns a value.

Okay I understand and I believe that I implemented the module wrong. Can you help me point out any mistakes in my setup process

Hi, I’ve been using Suphi’s datastore a good amount without any issue.

This is how my setup is in a single script (simplified):

local dataDebounce = {}
local function DataStateChanged(player,dataStore)
	while dataStore.State == false and player do
		if dataStore:Open(con.dataStoreTemplate) ~= con.dataStoreModule.Response.Success then task.wait(6) end
	end
	if player and dataStore.State == true and not dataDebounce[player] then
		dataDebounce[player] = true

		-- Custom things to do such as initializing something or doing something special for your game
		
		-- Lastly load their character
		player:LoadCharacter()
		dataDebounce[player] = nil
	end
end

players.PlayerAdded:Connect(function(player)
	
	-- Custom things to do...
	
	-- Initialize player datastore
	local dataStore = con.dataStoreModule.new("Player",player.userId)
	dataStore.StateChanged:Connect(function()
		DataStateChanged(player,dataStore)
	end)
	DataStateChanged(player,dataStore)
end)

players.PlayerRemoving:Connect(function(player)
	
	-- Custom things to do...
	
	-- Cleanup player datastore
	local dataStore = con.dataStoreModule.find("Player",player.userId)
	if dataStore ~= nil then dataStore:Destroy() end
end)

hey what is the keyword con? I am trying to implement your setup however there are unknown variables.

Can you provide the entire script rather than a simplified version. Also thank you so much for your help I really appreciate it <3

now that I am reading the code more I believe con may be the location of your datastore module copy.

Whoops, con is just my modulescript with a lot of variables. It is the same as

DataStoreModule = require(script["Main Module"])

I cannot give you the entire script, since it has a lot of my game specific code.

Whenever I want to receive the datastore in any other script or function, not connected to the playeradded event I do

-- Something something
-- Get datastore
local dataStore = con.dataStoreModule.find("Player",player.UserId)
if dataStore == nil or dataStore.State ~= true then return end
-- Don't go to this line if datastore is not a thing.

okay so I have implemented your setup process (thank you.)

How would I retrieve the data of a player?

I am currently trying to run the code:

local DataStoreModule = require(script["Main Module"])
local dataStore = DataStoreModule.new("Player", userId)

-- open and check for success

if dataStore:Open() ~= "Success" then
	-- not successful
	error("issue opening")
else
	-- Is successful
	print(dataStore.Value[key]) -- print value
	dataStore:Destroy()	
end	

If you do the setup I did in my first reply, then in any other function or script where you have a player, and want the datastore do this:

-- Get datastore
local dataStore = con.dataStoreModule.find("Player",player.UserId)
if dataStore == nil or dataStore.State ~= true then return end
-- You can then use dataStore.Value to get all the player's data.

okay thank you very mcuh I am going to try this:

local dataStore = DataStoreModule.find("Player",userId)

if dataStore == nil or dataStore.State ~= true then 
	error("failed to process")
else
	print("successfully retrieved " .. dataStore)
end

Also make sure that the DataStore modulescript is located in ServerStorage, no reason to have it elsewhere, just good practice to have it located there. Also consider renaming it from Main_module to DataStore, so you in fact know what it is, whenever you look into your ServerStorage.

thank you so much for the help. I believe I have it working but I would like you to verify i did everything right before I go.

here is the final code (from your VERY helpful feedback and from the tutorial knowledge)


– Variables

local DataStoreModule = require(workspace["Main Module"])
local dataDebounce = {}
local template = {
	hi = 0
}

– Functions

local function DataStateChanged(player,dataStore)
	while dataStore.State == false and player do
		if dataStore:Open(template) ~= DataStoreModule.Response.Success then task.wait(6) end
	end
	if player and dataStore.State == true and not dataDebounce[player] then
		dataDebounce[player] = true

		-- Lastly load their character
		player:LoadCharacter()
		dataDebounce[player] = nil
	end
end

– EVENTS

game.Players.PlayerAdded:Connect(function(player)
	
	-- load data
	local dataStore = DataStoreModule.new("Player",player.UserId)
	dataStore.StateChanged:Connect(function()
		DataStateChanged(player,dataStore)
	end)
	DataStateChanged(player,dataStore)
	
	
	-- attempt to retrieve data
	local dataStore = DataStoreModule.find("Player",player.UserId)
	if dataStore == nil or dataStore.State ~= true then 
		error("failed to process")
	else
		print(dataStore.Value["hi"]) -- GET current value
		dataStore.Value["hi"] = 999 -- SET value
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	-- Cleanup player datastore
	local dataStore = DataStoreModule.find("Player",player.UserId)
	if dataStore ~= nil then dataStore:Destroy() end
end)

You can try and update “hi” while the game is playing, in a random while-loop script. Check if the datastore is saving and loading correctly .

-- Get datastore
local dataStore = con.dataStoreModule.find("Player",player.UserId)
if dataStore == nil or dataStore.State ~= true then return end
-- You can then use dataStore.Value to get all the player's data.
dataStore.Value.hi += 1 -- This adds +1 to the current value of hi, just make sure that hi is a number.
1 Like

I used it and its working well.

Do you know how I would retrieve data of a user who isn’t in the server though?

If you know their userid, then you know what datastore to look for.

You just need to open it whenever you want to use it, and close it after use, as you did with your playeradded event and playerremoving event. :slight_smile:

How would i save a global list like a ban table

Hi, instead of

local dataStore = DataStoreModule.new("Player",player.UserId)

You could perhaps do?

local dataStore = DataStoreModule.new("Global_Bans",1)

Number two variable (in this case “1”) is just the key to a specific datastore (which is Player on the first example, and Global_Bans on this example), You can then just store each name that you want banned in that datastore.