Hey! So I made a saving system for swords that player own. Meaning whenever I add a new sword, I don’t have to put it in a table of data, it will automatically do that with a For Loop. However while this works, I have a feeling it is a bad practice, since it relies on using Indexes to find the boolean value of an owned sword. I was trying to have ‘table.insert’ insert a dictionary but it didn’t seem to work.
CODE:
local SwordDataStore = game:GetService("DataStoreService"):GetDataStore("SwordDataStores")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "OwnedSwords"
folder.Parent = player
for _, sword in ipairs(game.ServerStorage.Swords:GetChildren()) do
local Ownedsword = Instance.new("BoolValue")
Ownedsword.Name = sword.Name
Ownedsword.Parent = folder
end
local SwordData = player:WaitForChild("OwnedSwords"):GetChildren()
local data
local success, errorMessage = pcall(function()
data = SwordDataStore:GetAsync(player.UserId.."_OwnedSword")
end)
if success then
for i, SwordLoad in ipairs(SwordData) do
SwordLoad.Value = data[i]
end
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local SwordData1 = player:WaitForChild("OwnedSwords"):GetChildren()
local data = {}
for i, SwordBool in ipairs(SwordData1) do
table.insert(data, i, SwordBool.Value)
end
print(data)
local success, errorMessage = pcall(function()
SwordDataStore:SetAsync(player.UserId.."_OwnedSword", data)
end)
if success then
print("Data Saved")
else
warn(errorMessage)
end
end)