I wanted to save a table showing the trails that a person own and whether they had equipped the trails before they left so that when they rejoin, the trails they previously equip will be equipped again. When I tried to save the trails that was previously equip, the data doesn’t save. However, when I just save all the trails that the person own only, the data saves. May I know what is wrong and how can I fix it? Here is my code below
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("Trails")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = player.UserId.."-trails"
folder.Parent = player
local hasloaded = Instance.new("BoolValue")
hasloaded.Name = "hasloaded"
hasloaded.Value = false
hasloaded.Parent = player
local success,errormessage = pcall(function()
local data = datastore:GetAsync(player.UserId)
if data ~= nil then
local trailsfolder = game.ReplicatedStorage.Trails
local trails = trailsfolder:GetChildren()
for i,ownedtrails in pairs(data) do
for index,trail in pairs(trails) do
if trail[2] == ownedtrails then
local shopgui = player:WaitForChild("PlayerGui"):WaitForChild("Shop")
local shopframe = shopgui:WaitForChild("ShopFrame")
local trailsframe = shopframe:WaitForChild("Trailsframe")
local information = trailsframe:FindFirstChild(trail.Name.."information")
local buybutton = information:WaitForChild("Mainbg"):WaitForChild("Buy")
local equipbutton = information:WaitForChild("Mainbg"):WaitForChild("Equip")
if information.Name == trail[1] then
equipbutton.Name = "Unequip"
end
buybutton.Visible = false
local trailbought = trail:Clone()
trailbought.Parent = folder
equipbutton.Visible = true
hasloaded = true
end
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local folder = player:WaitForChild(player.UserId.."-trails")
local trailsowned = {}
local success,errormsg = pcall(function()
for i,v in pairs(folder:GetChildren()) do
local shopgui = player:WaitForChild("PlayerGui"):WaitForChild("Shop")
local shopframe = shopgui:WaitForChild("ShopFrame")
local trailsframe = shopframe:WaitForChild("Trailsframe")
local information = trailsframe:FindFirstChild(v.Name.."information")
local buybutton = information:WaitForChild("Mainbg"):WaitForChild("Buy")
local equipbutton = information:WaitForChild("Mainbg"):WaitForChild("Equip")
if equipbutton.Name == "Unequip" then
trailsowned[1] = information.Name
print(trailsowned)
else
trailsowned[1] = "None"
print(trailsowned)
end
print(v.Name)
trailsowned[2] = v.Name
print(trailsowned)
end
print(trailsowned)
datastore:SetAsync(player.UserId,trailsowned)
end)
end)
game:BindToClose(function()
for index,player in pairs(game.Players:GetChildren()) do
local folder = player:WaitForChild(player.UserId.."-trails")
local trailsowned = {}
local success,errormsg = pcall(function()
for i,v in pairs(folder:GetChildren()) do
local shopgui = player:WaitForChild("PlayerGui"):WaitForChild("Shop")
local shopframe = shopgui:WaitForChild("ShopFrame")
local trailsframe = shopframe:WaitForChild("Trailsframe")
local information = trailsframe:FindFirstChild(v.Name.."information")
local buybutton = information:WaitForChild("Mainbg"):WaitForChild("Buy")
local equipbutton = information:WaitForChild("Mainbg"):WaitForChild("Equip")
if equipbutton.Name == "Unequip" then
trailsowned[1] = information.Name
print(trailsowned)
else
trailsowned[1] = "None"
print(trailsowned)
end
print(v.Name)
trailsowned[2] = v.Name
print(trailsowned)
end
print(trailsowned)
datastore:SetAsync(player.UserId,trailsowned)
end)
end
end)