Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Data is not saving and pcall is not running?

Asked by
tjtorin 172
5 years ago
Edited 5 years ago

I wanted the player data to save when they leave the game but it was not working so I added a pcall and I am getting no response from it.

local Data = game:GetService("DataStoreService"):GetDataStore("PlayerDataV0")

local prefix = "user_"

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Value = 0
    cash.Parent = leaderstats

    local size = Instance.new("NumberValue")
    size.Name = "Size"
    size.Value = 1.07
    size.Parent = leaderstats

    local rebirths = Instance.new("NumberValue")
    rebirths.Name = "Rebirths"
    rebirths.Value = 0
    rebirths.Parent = leaderstats

    local plrData = Data:GetAsync(prefix..tostring(player.UserId))

    if plrData then
        cash.Value = plrData[1] or 0
        size.Value = plrData[2] or 1.07
        rebirths.Value = plrData[3] or 0
    else
        Data:SetAsync(prefix..tostring(player.UserId),{
            cash.Value,
            size.Value,
            rebirths.Value
        })
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    warn("Saving Data...")
    local function saveData()
        Data:SetAsync(prefix..tostring(player.UserId),{
            player.leaderstats.Cash.Value,
            player.leaderstats.Size.Value,
            player.leaderstats.Rebirths.Value
        })
    end
    local success, message = pcall(saveData)
    if success then
        warn("Successfully saved "..player.Name.."'s data!")
    else
        warn("An error ccurred: "..message)
    end
end)

Answer this question