Datastore throws error first time player enters game but no error after player comes in for second time

I’m trying to figure out if this is a big deal or not.

I have a Datastore with a couple values in it. I am using an Array.

This is how I load it.

local data  = {}
local success, errormessage = pcall(function()
data = clicksdata:GetAsync(player.UserId)
end)

if success then
print ("load success")
lifekills.Value = data[1] or 0
lifepoints.Value = data[2] or 0

    else
print (errormessage)
end

Thing is, I get an error when a player enters the game for the first time and I understand why, because the array isn’t saved yet… it is NIL. When the player exits the game for first time it creates the array and when he reenters it exists, and there is no error anymore.

Is this normal? I figured by putting the “or” in lifekills.Value = data[1] or 0,
it would set it to 0 if nil.

Any ideas?

1 Like

That’s because you didn’t check if there was data in the key yet:

if success then

You have to add an extra statement:

if success then
    if data then
    
    else
        print("No Data")
    end
end

This is because there is no data entry when a player just joins.

2 Likes