'How to fix: attempt to index nil with 'entered'

So basically, the script is supposed to load text after the intro screen disappears, not sure what's wrong with it because it worked back in 2014 :/

The Code:

if true then
    script.Parent.slowsleigh:Play()
    fadegui({intro.load},{0},{1},30)
    wait(1)

    local titles = intro.titles
    local text1 = titles.title1.Text
    local text2 = titles.title2.Text
    local text3 = titles.title3.Text
    local text4 = titles.title4.Text
    if GameData.entered == 1 then
        text1 = "And you came back."
        text2 = "You can't change the past."
        text3 = "Pathetic."
    end

    titles.title1.Text = ""
    titles.title2.Text = ""
    titles.title3.Text = ""
    titles.title4.Text = ""
    --[
    titles.title1.Visible = true
    for i = 1, #text1 do
        titles.title1.Text = string.sub(text1,1,i)
        wait(0.05 + math.random(-5,10)/200)
    end

And heres the script inside of ServerScriptServices

local data = game:GetService("DataStoreService"):GetGlobalDataStore()
local dataVersion = 2

local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
    local store = data:GetAsync(tostring(player.UserId))
    return store
end

local set = Instance.new("RemoteEvent",workspace)
set.Name = "SetData"
set.OnServerEvent:connect(function(player,newData)
    local store = data:GetAsync(tostring(player.UserId))
    for i,v in pairs(newData) do
        store[i] = v
    end
    data:SetAsync(player.UserId, store)
end)

The Error:

  17:48:57.908  Players.Trl0_P90.PlayerGui.maingui.mainscript:758: attempt to index nil with 'entered'  -  Client - mainscript:758


Solution 1:[1]

The error is telling you that you are trying to access GameData.entered, when GameData is nil.

From the code you have shared, your issue appears to be that you are accessing data from the data store and not accounting for the fact that it might not exist. Take a look at the docs for GlobalDataStore:GetAsync() :

This function returns the latest value of the provided key and a DataStoreKeyInfo instance. If the key does not exist or if the latest version has been marked as deleted, both return values will be nil.

This could be caused by a new player joining, and there being no default data. So, one thing you could do is set up some default data in case there's a new player:

local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
    local store = data:GetAsync(tostring(player.UserId))

    -- if there is no data, it might be a new player!
    -- provide some new player data
    if not store then
        store = {
            entered = 1,
            foo = true,
            bar = "blah",
        }
    end
    return store
end

This way, the function will always return the correctly shaped data.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kylaaa