Can you please rate my datastore script?

Can you please rate my datastore script?
It is working fine but I want your feedback to make it better and prevent It from sending too much requests.


local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local statsfolder = Instance.new("Folder", Player)
	statsfolder.Name = "stats"
	
	local level = Instance.new("NumberValue", leaderstats)
	level.Name = "Level"
	
	local currentxp = Instance.new("NumberValue", statsfolder)
	currentxp.Name = "CurrentXP"
	
	local cash = Instance.new("NumberValue", statsfolder)
	cash.Name = "Cash"


	local Success, Error
	local playerUserId = "Player_" .. Player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then
		print("Data: ")
		print(data)
		level.Value = data['Level']
		currentxp.Value = data['CurrentXP']
		cash.Value = data['Cash']
		game.ReplicatedStorage.XPChanged:FireClient(Player)
	else
		print("No data!")
		level.Value = 1
		currentxp.Value = 0
		cash.Value = 500
		game.ReplicatedStorage.XPChanged:FireClient(Player)
	end
	


	local function create_table(player)

		local player_stats = {}

		for _, stat in pairs(player.leaderstats:GetChildren()) do

			player_stats[stat.Name] = stat.Value

		end
		for _, stat in pairs(player.stats:GetChildren()) do

			player_stats[stat.Name] = stat.Value

		end

		return player_stats

	end

		Player.stats.Cash.Changed:Connect(function()
			local player_stats = create_table(Player)
wait(1)

			playerData:SetAsync(playerUserId, player_stats) --Saves player data
	end)
	
	Player.stats.CurrentXP.Changed:Connect(function()
		local player_stats = create_table(Player)
		wait(1)

		playerData:SetAsync(playerUserId, player_stats) --Saves player data
	end)
	
	Player.leaderstats.Level.Changed:Connect(function()
		local player_stats = create_table(Player)
		wait(1)

		playerData:SetAsync(playerUserId, player_stats) --Saves player data
	end)



		
game.ReplicatedStorage.UpdateLevelLeaderstat.OnServerEvent:Connect(function(Player, Amount)
	Player.leaderstats.Levels.Value = Amount
	end)
	
end)


Thank you for your feedback!

Looks like a great base.

Some feedback:

  1. Datastore requests may error, which could cause your script to break. Implementing error handing and/or a retry system could save the game from breaking every once in a while.
  2. It seems like you have a remote event that allows the client to tell the server what to set for the values. If this is important data, you probably don’t want the client telling the server. A hacky user could have the client tell the server any value they want. I’d suggest having the server do all of the important logic and never listen to the client.
  3. Implement a system that doesn’t try to save every time a value changes. You might hit rate limits if you save and read too frequently to datastores. I’d suggest only saving in specific cases: when a player leaves, when the game is shutting down, when a player spends robux on something, and once every 5 minutes if the data is has been updated incase other methods ever fail.
1 Like

Thank you for your feedback!

  1. Can pcall resolve that?

  2. I would like tego do that, but sadly i don’t know how to do that.

  3. I don’t know how to do that too😅

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.