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

Attempt to index nil with 'Upgrades'?

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)

7 comments

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

0

So do I delete the second loop?

0

Delete the first. Basically, the inside of the BindToClose function should look exactly like the inside of the PlayerRemoving function

0

Ok!

0

I still get the same error

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

0

Thanks it worked

1 answer

✓ Accepted answer

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)

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