Is there / What is a more effective way to script this?

The purpose of this code is to save and load a number of bool values. This number is determined by the number of children in another Gui (ListGui.MainFrame.ListFrame).

As of now, the script seems rather messy and entangled, prone to errors.

Here is the code:

--{VARIABLES}--
local DataStoreService = game:GetService("DataStoreService")
local PhobiaCompletedStore = DataStoreService:GetDataStore("PhobiasCompleted")
local NoPhobias = -2
local Data


--{LOAD DATA FUNCTION}--
local function LoadData(Player,i)
	
	local PhobiaBool = Player.Phobias:FindFirstChild(tostring("Phobia"..i))
	
	--{LOAD DATA}--
	local Success,ErrorMessage = pcall(function()
		Data = PhobiaCompletedStore:GetAsync(Player.UserId..PhobiaBool.Name)
	end)
	
	--{ERROR CHECK}--
	if Success then
		PhobiaBool.Value = Data
		print("Successfully Loaded "..Player.Name.."'s Phobia Data")
	else
		print("Unsuccessfully Loaded "..Player.Name.."'s Phobia Data")
		warn(ErrorMessage)
	end
	
end



--{SAVE DATA FUNCTION}--
local function SaveData(Player,i)
	
	local PhobiaBool = Player.Phobias:FindFirstChild(tostring("Phobia"..i))
	
	--{SAVE DATA}--
	local Success,ErrorMessage = pcall(function()
		PhobiaCompletedStore:SetAsync(Player.UserId..PhobiaBool.Name,PhobiaBool.Value)
	end)	
	
	--{ERROR CHECK}--
	if Success then
		print("Successfully Saved "..Player.Name.."'s Phobia Data")
	else
		print("Unsuccessfully Saved "..Player.Name.."'s Phobia Data")
		warn(ErrorMessage)
	end
	
end



--{PLAYER JOINED EXECUTE}--
game.Players.PlayerAdded:Connect(function(Player)
	
	--{DETERMINE NUMBER OF DATA}--
	local ListGui = Player.PlayerGui:WaitForChild("ListGui")
	
	for i ,v in pairs(ListGui.ListFrame:GetChildren()) do
		NoPhobias = NoPhobias + 1
	end
	
	--{CREATING DATA FOLDER}--
	local PhobiaFolder = Instance.new("Folder")
	PhobiaFolder.Name = "Phobias"
	PhobiaFolder.Parent = Player
	
	--{CREATING DATA STORAGE}--
	for i = 1,NoPhobias do
		local Phobia = Instance.new("BoolValue")
		Phobia.Name = "Phobia"..i
		Phobia.Parent = PhobiaFolder
		--{LOAD DATA}--
		LoadData(Player,i)
	end
	
end)


--{PLAYER LEFT EXECUTE}--
game.Players.PlayerRemoving:Connect(function(Player)
	
	for i = 1,NoPhobias do
		--{SAVE DATA}--
		SaveData(Player,i)
	end
	
end)

The default -2 on the number of phobias is to counter the UiListLayout and ListScript from effecting the final sum of data.

Here is the layout when ran:

Any aid would be greatly appreciated, thank you!

2 Likes

Seems fine? Does it actually break, or do you just think it might break at some point?

As for dealing with DataStoreService errors, wrapping your DSS calls in a pcall is a good first step but not actually that helpful. In some cases a call will be successful on the second or some subsequent attempt, so in some cases it’s worth it to keep trying. If data loading fails you can just kick the player and have them try again, but if data saving fails they’re going to loose progress in your game and that’s arguably a worse experience. So you might want to at least try a couple of times before giving up on saving.

There’s an example of how you can do that here: Documentation - Roblox Creator Hub

Also check out

2 Likes