Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I error-proof this datastore saving system?

SaveData = function(plr)
	local lstats = plr:FindFirstChild("leaderstats")
	if lstats~=nil then
		local Level = lstats:FindFirstChild("Level")
		local EXP = plr:FindFirstChild("EXP")
		local Coins = lstats:FindFirstChild("Coins")
		if Level~=nil and EXP~=nil and Coins~=nil then
			local tablestats = {
				Level.Value,
				EXP.Value,
				Coins.Value
			}
			local GetData = PlayerStats:GetAsync(plr.UserId)
			if GetData then
				PlayerStats:UpdateAsync(plr.UserId, function() return tablestats end)
			else
				PlayerStats:SetAsync(plr.UserId,tablestats)
			end
		end
	end
end

LoadData = function(plr,Level,EXP,Coins)
	local GetData = PlayerStats:GetAsync(plr.UserId)
	if GetData then
		Level.Value = GetData[1]
		EXP.Value = GetData[2]
		Coins.Value = GetData[3]
	else
		Level.Value = 1
		EXP.Value = 0
		Coins.Value = 0
	end
end

This is my current script for saving stats of players (Levels, EXP and Coins) it works perfectly fine so far and I'm happy with it, but I know and have been told datastore errors occur and I should prepare for them. How would I make these datastore functions be able to handle errors that may occur so I could possibly kick the player if their data could be removed or not saved?

1 answer

Use pcalls

The wiki also has a great example/tutorial of error handling for Data Stores here.

This discussion is preserved as it was. New answers, comments, and votes are closed.