#need help with script to save data

1 messages · Page 1 of 1 (latest)

fervent barn
#

local DataStoreService = game:GetService("DataStoreService")
local collectedDataStore = DataStoreService:GetDataStore("Collected")
local replicatedStorage = game:GetService("ReplicatedStorage")
local squareList = replicatedStorage:WaitForChild("BoolSquares")

game.Players.PlayerAdded:Connect(function(player)
local SquaresCollected = Instance.new("Folder",player)
SquaresCollected.Name = "SquaresCollected"

-- load collected square
local success, errormessage = pcall(function()
local Squares = collectedDataStore:GetAsync("User-"..player.UserId)
if Squares then 
    for i, v in pairs(Squares) do 
        local SquareFound = squareList:FindFirstChild(v)
        if SquareFound then 
            SquareFound:Clone().Parent = SquaresCollected
         end
     end
 end

end)
end)

game.Players.PlayerRemoving:Connect(function(player)
-- saves collected square
local success, errormessage = pcall(function()
local collectedSave = {}
for i, Square in pairs(player.SquaresCollected:GetChildren()) do
if Square then
table.Insert(collectedSave,Square.Name)

        end
    end
    collectedDataStore:SetAsync("User-"..player.UserId,collectedSave)
end)

end)

game:BindToClose(function(player)
-- saves collected square
local success, errormessage = pcall(function()
local collectedSave = {}
for i, Square in pairs(player.SquaresCollected:GetChildren()) do
if Square then
table.Insert(collectedSave,Square.Name)

        end
    end
    collectedDataStore:SetAsync("User-"..player.UserId,collectedSave)
end)

end)

the script is functional but data does not save when you leaqve and rejoin the game anybody know how to fix?

cerulean narwhal
#

add prints to check what's running, what's being saved etc