Is there any improvements I can do to my simple datastore

Your datastore isn’t the best to store data securely here is an example of mine.
I think mine is a good datastore.

local DSS = game:GetService("DataStoreService")
local CashData = DSS:GetDataStore("CashData")
local GemsData = DSS:GetDataStore("GemsData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local gemstats = Instance.new("Folder", player)
	gemstats.Name = "gems"

	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"

	local gems = Instance.new("IntValue", gemstats)
	gems.Name = "Gems"

	local playerId = "Player_"..player.UserId

	local cashdata
	local gemsdata
	local success, errormessage = pcall(function()
		cashdata = CashData:GetAsync(playerId)
		gemsdata = GemsData:GetAsync(playerId)
	end)

	if success then
		cash.Value = cashdata
		gems.Value = gemsdata
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerId = "Player_"..player.UserId

	local cashdata = player.leaderstats.Cash.Value
	local gemsdata = player.gems.Gems.Value

	local success, errormessage = pcall(function()
		CashData:SetAsync(playerId, cashdata)
		GemsData:SetAsync(playerId, gemsdata)
	end)

	if success then
		print("Saved")
	else
		print("There was an error")
		warn(errormessage)
	end
end)

1 Like