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

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

Asked by
Soban06 410 Moderation Voter
4 years ago

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

0
What line is error at? zuup123 99 — 4y

2 answers

Log in to vote
0
Answered by
0msh 333 Moderation Voter
4 years ago

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)
Ad
Log in to vote
0
Answered by 4 years ago

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.

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. Soban06 410 — 4y

Answer this question