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!