DataStoreCache Update - Safer Data Saving!

Hello programmers! I have finally fixed some vulnerabilities in the DataStoreCache module as well as added more functionality. Here’s a quick changelog:

  • Used pcall for safer Datastore requests.
  • Added support for multiple scripts to use the same cache.
  • You can now construct DataStoreCache in multiple sides of the script and it will return the same Cache instance.

Quick example on how to use it across Scripts:

-- Script 1
local dataStoreCache = require(script.Parent)
local cache = dataStoreCache.new("KEY", "OPTIONAL SCOPE")
-- Script 2
local dataStoreCache = require(script.Parent)
local cache = dataStoreCache.new("KEY", "OPTIONAL SCOPE")

It returns the same cache in both scripts.

You can grab a copy of the module here:

Gold Currency Example

[This example can be found in the Examples folder inside of the ModuleScript!

local players = game:GetService("Players")

local dataStoreCache = require(script.Parent.Parent)

players.PlayerAdded:Connect(function(plr)
	-- Get the player's datastore.
	local ds = dataStoreCache.new(tostring(plr.UserId))
	
	-- Create a leaderstat.
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	local gold = Instance.new("NumberValue")
	gold.Name = "Gold"
	gold.Parent = leaderstats
	
	local save = ds:get("gold")
	if(save ~= nil) then
		-- Player has a save.
		gold.Value = save
	else
		-- Starting allowance?
		gold.Value = 10
	end
end)

players.PlayerRemoving:Connect(function(plr)
	-- Get the player's datastore.
	local ds = dataStoreCache.new(tostring(plr.UserId))
	local gold = plr:WaitForChild("leaderstats"):WaitForChild("Gold")
	ds:set("gold", gold.Value)
	ds:flushAll()
end)

Thanks for reading my post and maybe checking out my module!
Please contact me if you have any constructive criticism/bug reports. It helps me work out the kinks in my code. :blue_heart:

:wave:

1 Like

This topic was automatically closed after 1 minute. New replies are no longer allowed.