hello, i've been having issues on how to save data. This is my second time using datastore so I definitely don't know what I'm doing. The error occurs at line 39, "_CENTS.Value = data[1] or 100".
local DataStoreService = game:GetService("DataStoreService") local DataStore1 = DataStoreService:GetDataStore("DataStore1") game.Players.PlayerAdded:Connect(function(player) repeat wait() until player ~= nil local _CENTS = game.ServerStorage.Currencys[player.Name].Currency.Cents local _CURRENTSTEPSTOMULTIPLIER = game.ServerStorage.Currencys[player.Name].UI_Mechanics.CurrentStepsToMultiplier local _CURRENTENERGY = game.ServerStorage.Currencys[player.Name].UI_Mechanics.Current_Energy local _CURRENTMULTIPLIER = game.ServerStorage.Currencys[player.Name].UI_Mechanics.Current_Multiplier local _CURRENTSTAMINA = game.ServerStorage.Currencys[player.Name].UI_Mechanics.Current_Stamina local _MAXENERGY = game.ServerStorage.Currencys[player.Name].UI_Mechanics.Max_Energy local _STAMINAMAX = game.ServerStorage.Currencys[player.Name].UI_Mechanics.Stamina_Max local _STEPSTONEXTMULTIPLIER = game.ServerStorage.Currencys[player.Name].UI_Mechanics.StepsToNextMultiplier local valuesToSave = {_CENTS.Value, _CURRENTSTEPSTOMULTIPLIER.Value, _CURRENTENERGY.Value, _CURRENTSTAMINA.Value, _MAXENERGY.Value, _STAMINAMAX.Value, _STEPSTONEXTMULTIPLIER.Value, } local playerUserId = "player_"..player.UserId local data -- A variable to assign the data to --[[ load data --]] local success, errormessage = pcall(function() data = DataStore1:GetAsync(playerUserId, valuesToSave) end) if success then _CENTS.Value = data[1] or 100 _CURRENTSTEPSTOMULTIPLIER.Value = data[2] or 0 _CURRENTENERGY.Value = data[3] or 0 _CURRENTSTAMINA.Value = data[4] or 0 _MAXENERGY.Value = data[5] or 50 _STAMINAMAX.Value = data[6] or 100 _STEPSTONEXTMULTIPLIER.Value = data[7] or 2 elseif errormessage then print(errormessage) end end)
If loading doesn't error but saved data for the key doesn't exist (consider a player joining for the first time - they have no data saved), GetAsync
would return nil
. data
would be nil
in that case, and so data[1]
would give that error. A default data
can be given if it's nil
. With your current defaulting, the default data
there can be an empty table.
Don't put _CENTS.Value
by the " { "
It should be like this:
local valuesToSave = { _CENTS.Value, _CURRENTSTEPSTOMULTIPLIER.Value, _CURRENTENERGY.Value, _CURRENTSTAMINA.Value, _MAXENERGY.Value, _STAMINAMAX.Value, _STEPSTONEXTMULTIPLIER.Value, }