is this a good way to save trails?
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("StatsDataStore")
local ServerStorage = game:GetService("ServerStorage")
Players.PlayerAdded:Connect(function(player)
local PlayerTrails = Instance.new("Folder",player)
PlayerTrails.Name = "PlayerTrails"
local TrailsFile = ServerStorage:WaitForChild("Trails")
-- load saved data
local data
local success, ErrorMsg = pcall(function()
data = DataStore:GetAsync("player_"..player.UserId)
end)
if success and data then
-- load trails
for _, trail in pairs(TrailsFile:GetChildren()) do
for _, TrailName in pairs(data.trails) do
if trail.Name == TrailName then
local TrailClone = trail:Clone()
TrailClone.Parent = PlayerTrails
print(trail.Name)
end
end
end
else
print("there was an error while loading player data")
warn(ErrorMsg)
end
local PlayerGui = player:WaitForChild("PlayerGui")
local ScrollingFrame = PlayerGui:WaitForChild("TrailsGui"):WaitForChild("TrailsFrame"):WaitForChild("ScrollingFrame")
for _,trail in pairs(PlayerTrails:GetChildren()) do
for _, TrailImage in pairs(ScrollingFrame:GetChildren()) do
if TrailImage:IsA("ImageLabel") and TrailImage.Name == trail.Name then
TrailImage.EquipButton.Text = "Equip"
TrailImage.PriceText.Visible = false
end
end
end
end)
Players.PlayerRemoving:Connect(function(player)
local Money = player.leaderstats.Money
local Wins = player.leaderstats.Wins
local PlayerTrails = player:WaitForChild("PlayerTrails")
local data
data.trails = data.trails or {}
if #PlayerTrails:GetChildren() > 0 then
for i,trail in pairs(PlayerTrails:GetChildren()) do
if table.find(data.trails, trail.Name) == nil then
print(trail.Name)
table.insert(data.trails, trail.Name)
end
end
end
local success, ErrorMsg = pcall(function()
DataStore:SetAsync("player_"..player.UserId, data)
end)
if success == false then
print("there was an error while saving players data")
warn(ErrorMsg)
end
end)