Hi, so let me start off by saying I'm pretty new to datastores. As seen below, I have a leaderstat datastore. It works, but I feel like this isn't really efficient. I was editing the script the other day and typoed, it then caused the datastore to throttle and it caused the stats to reset. Should I use a pcall to check for errors when the player joins the game? Also, will this work with FE 100% of the time? I could use ideas and suggestions on how to fully optimize the use of this datastore. Any comment helps and let me know if you have any questions! The script is in a server script.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("LevelSaveSystem")
local ds2 = datastore:GetDataStore("CreditsSaveSystem")
local ds3 = datastore:GetDataStore("XPSaveSystem")
local ds4 = datastore:GetDataStore("WinSaveSystem")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	local credits = Instance.new("IntValue", folder)
	credits.Name = "Credits"
	local xp = Instance.new("IntValue", folder)
	xp.Name = "XP"
	local wins = Instance.new("IntValue", folder)
	wins.Name = "Wins"
	
	level.Value = ds1:GetAsync(plr.UserId) or 0
	ds1:SetAsync(plr.UserId, level.Value)
	
	credits.Value = ds2:GetAsync(plr.UserId) or 0
	ds2:SetAsync(plr.UserId, credits.Value)
	
	xp.Value = ds3:GetAsync(plr.UserId) or 0
	ds3:SetAsync(plr.UserId, xp.Value)
	
	wins.Value = ds4:GetAsync(plr.UserId) or 0
	ds4:SetAsync(plr.UserId, wins.Value)
	
	
	level.Changed:Connect(function()
		ds1:SetAsync(plr.UserId, level.Value)
	end)
	
	credits.Changed:Connect(function()
		ds2:SetAsync(plr.UserId, credits.Value)
	end)
	
	xp.Changed:Connect(function()
		ds3:SetAsync(plr.UserId, xp.Value)
	end)
	
	wins.Changed:Connect(function()
		ds4:SetAsync(plr.UserId, wins.Value)
	end)	
end)

2 comments

0

Module scripts, tables, pcall, and don't save if there was an ERROR whilst loading data after x tries. Those are the basics.

0

Thanks, I get the pcall and the don't save, but could you explain how module scripts and tables would make this script better? I'm confused on those