Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does the output say "attempt to index nil with 'Position'"?

I am making an obby and I have a Script inside ServerScriptService.

When I run the game, the output says "ServerScriptService.checkpointHandler:22: attempt to index nil with 'Position'" Why does it say that?

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


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

    local Checkpoint = Instance.new("IntValue", leaderstats)
    Checkpoint.Name = "Checkpoint"
    Checkpoint.Value = 1

    local coins = Instance.new("IntValue", leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    --Checkpoint Section
    player.CharacterAdded:Connect(function(character)

        repeat wait() until player.character ~= nil
        local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value)
        character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 2, 0))

    end)

    -- Data Store Section

    local playerUserId = "Player_"..player.UserId
    print(playerUserId)

    -- Load Data

    local data
    local success, errormessage = pcall(function()
         data = myDataStore:GetAsync(playerUserId)

    end)


    if success then
        if data then
        coins.Value = data.Coins
        Checkpoint.Value = data.Checkpoint
        -- Set our data equal to the current Coins
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = {

    Coins = player.leaderstats.Coins.Value;
    Checkpoint = player.leaderstats.Checkpoint.Value;

    }

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

        if success then
            print("Data successfully saved!")
        else
            print("There was an error saving the data!")
            warn(errormessage)  
        end

end)

Please do help

Thanks

1 comment

0

What line is error at?

2 answers

this should fix it. i believe your error is at line 22

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


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

    local Checkpoint = Instance.new("IntValue", leaderstats)
    Checkpoint.Name = "Checkpoint"
    Checkpoint.Value = 1

    local coins = Instance.new("IntValue", leaderstats)
    coins.Name = "Coins"
    coins.Value = 0

    --Checkpoint Section
    player.CharacterAdded:Connect(function(character)

        repeat wait() until player.character ~= nil
        local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value)
        character:WaitForChild("HumanoidRootPart").CFrame = (checkpoint.CFrame * CFrame.new(0,2,0))

    end)

    -- Data Store Section

    local playerUserId = "Player_"..player.UserId
    print(playerUserId)

    -- Load Data

    local data
    local success, errormessage = pcall(function()
         data = myDataStore:GetAsync(playerUserId)

    end)


    if success then
        if data then
        coins.Value = data.Coins
        Checkpoint.Value = data.Checkpoint
        -- Set our data equal to the current Coins
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local data = {

    Coins = player.leaderstats.Coins.Value;
    Checkpoint = player.leaderstats.Checkpoint.Value;

    }

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)

        if success then
            print("Data successfully saved!")
        else
            print("There was an error saving the data!")
            warn(errormessage)  
        end

end)

On line 22, you index checkpoint.Position, which means you are looking for the position value of checkpoint. However, the error says that you are indexing nil, which means that checkpoint is a nil value. I would use print(checkpoint) before line 22 to confirm if this is true, but local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value) is likely the source of the issue, as it is not properly returning what you want.

1 comment

0

The whole point is that if I write the Checkpoint Section script and the leaderboard script, it does exactly what I want but when I add the data store section below it, it causes the error.

This discussion is preserved as it was. New answers, comments, and votes are closed.