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

Is it safe using BoolValues to determine whether data failed or not?

Asked by 4 years ago
Edited 4 years ago
local DataStore = game:GetService("DataStoreService"):GetDataStore('NewStore')

local DefaultValue = 0

game.Players.PlayerAdded:Connect(function(Player)

    local Folder = Instance.new('Folder')
    Folder.Name = Player.UserId
    Folder.Parent = game.ServerStorage


    local Points_IntValue = Instance.new("IntValue")
    Points_IntValue.Name = 'Points'
    Points_IntValue.Parent = Folder

    local DataWasLoaded = Instance.new('BoolValue')
    DataWasLoaded.Name = 'DataWasLoaded'
    DataWasLoaded.Parent = Folder


    local S,E = pcall(function()
        return DataStore:GetAsync(Player.UserId)
    end)

    if S then

        if E and type(E) == 'number' then
            Points_IntValue.Value = E
            DataWasLoaded.Value = true -- Set Data loaded to true if it was succesfully loaded
        else

            Points_IntValue.Value = DefaultValue
            DataWasLoaded.Value = true -- Set Data loaded to true if it was succesfully loaded

        end

    else
        Points_IntValue:Destroy()
        DataWasLoaded.Value = false --Set Data loaded to false if it was failed

    end


end)

So Overall I just wanted to check if this was a good way to load data

Is there any improvements I can make?

Answer this question