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

Attempt to index nil with 'Upgrades'?

Asked by 2 years ago

For some reason on my upgrade saver's code just gives me an error on line 34 saying "Attempt to index nil with 'Upgrades' And here is the code

local dataService = game:GetService("DataStoreService")
local upgradeData = dataService:GetDataStore("upgradeStorage")
local Players = game:GetService("Players")

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

    local rebirthButtons = Instance.new("IntValue", upgradesFolder)
    rebirthButtons.Name = "rebirthButtons"
    rebirthButtons.Value = 0


    local UpgradesData = upgradeData:GetAsync(player.UserId)
    if UpgradesData ~=nil then
        rebirthButtons.Value = UpgradesData[1]
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local upgrades = {}
    for _, child in pairs(player.Upgrades:GetChildren()) do
        table.insert(upgrades,child.Value)
    end 

    pcall(function()
        upgradeData:SetAsync(player.UserId, upgrades)
    end)
end)

game:BindToClose(function(player)
    for _, Player in pairs(game.Players:GetChildren()) do
        local upgrades = {}
        for _, child in pairs(player.Upgrades:GetChildren()) do
            table.insert(upgrades,child.Value)
        end 

        pcall(function()
            upgradeData:SetAsync(player.UserId, upgrades)
        end)
    end
end)
0
Why is there another loop in the BindToClose function that runs through every player? You dont evel use the "Player", and it seems pointless to save a single player's data as many times as there are players WoTrox 345 — 2y
0
So do I delete the second loop? Dank_Switch 9 — 2y
0
Delete the first. Basically, the inside of the BindToClose function should look exactly like the inside of the PlayerRemoving function WoTrox 345 — 2y
0
Ok! Dank_Switch 9 — 2y
View all comments (3 more)
0
I still get the same error Dank_Switch 9 — 2y
0
Okay, turns out, the BindToClose isnt what i thought it is. It doesnt have a "player" argument. So try putting back the first loop, and use "Player" instead of "player" in the function WoTrox 345 — 2y
0
Thanks it worked Dank_Switch 9 — 2y

1 answer

Log in to vote
0
Answered by
WoTrox 345 Moderation Voter
2 years ago

Try this

local dataService = game:GetService("DataStoreService")
local upgradeData = dataService:GetDataStore("upgradeStorage")
local Players = game:GetService("Players")

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

    local rebirthButtons = Instance.new("IntValue", upgradesFolder)
    rebirthButtons.Name = "rebirthButtons"
    rebirthButtons.Value = 0


    local UpgradesData = upgradeData:GetAsync(player.UserId)
    if UpgradesData ~=nil then
        rebirthButtons.Value = UpgradesData[1]
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local upgrades = {}
    for _, child in pairs(player.Upgrades:GetChildren()) do
        table.insert(upgrades,child.Value)
    end 

    pcall(function()
        upgradeData:SetAsync(player.UserId, upgrades)
    end)
end)

game:BindToClose(function() -- no argument here
    for _, player in pairs(game.Players:GetChildren()) do -- changed "Player" to "player"
        local upgrades = {}
        for _, child in pairs(player.Upgrades:GetChildren()) do
            table.insert(upgrades,child.Value)
        end 

        pcall(function()
            upgradeData:SetAsync(player.UserId, upgrades)
        end)
    end
end)
Ad

Answer this question