Data not saving

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)
1 Like

Replying to bump up the post as no one replied

Hello, Here is my attempt on fixing your issue. If you have any questions, make sure you ask!

--|< Services >|--
local DataStoreService = game:GetService("DataStoreService");

--|< Variables >|--
local DataStore = DataStoreService:GetDataStore("Trails");

--|< Connections >|--
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 then
            local trailsFolder = game.ReplicatedStorage.Trails;
            local trails = trailsFolder:GetChildren();
            for _, ownedTrail in pairs(data) do
                for _, trail in pairs(trails) do
                    if trail.Name == ownedTrail 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.Name then
                            equipButton.Name = "Unequip";
                        end
                        buyButton.Visible = false;
                        local trailBought = trail:Clone();
                        trailBought.Parent = folder;
                        equipButton.Visible = true;
                        hasLoaded.Value = true;
                    end
                end
            end
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local folder = player:WaitForChild(player.UserId.."-trails");
    local trailsOwned = {};
    local success, errorMessage = pcall(function()
        for _, trail 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(trail.Name.."information");
            local buyButton = information:WaitForChild("Mainbg"):WaitForChild("Buy");
            local equipButton = information:WaitForChild("Mainbg"):WaitForChild("Equip");
            trailsOwned[1] = equipButton.Name == "Unequip" and information.Name or "None";
            trailsOwned[2] = trail.Name;
            print(trailsOwned);
        end
        print(trailsOwned);
        DataStore:SetAsync(player.UserId, trailsOwned);
    end)
end)

--|< Bind >|--
game:BindToClose(function()
    for _, player in pairs(game.Players:GetChildren()) do
        local folder = player:WaitForChild(player.UserId.."-trails");
        local trailsOwned = {};
        local success, errorMessage = pcall(function()
            for _, trail 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(trail.Name.."information");
                local buyButton = information:WaitForChild("Mainbg"):WaitForChild("Buy");
                local equipButton = information:WaitForChild("Mainbg"):WaitForChild("Equip");
                trailsOwned[1] = equipButton.Name == "Unequip" and information.Name or "None";
                trailsOwned[2] = trail.Name;
                print(trailsOwned);
            end
            print(trailsOwned);
            DataStore:SetAsync(player.UserId, trailsOwned);
        end)
    end
end)

Ok I will try and do it