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
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
So do I delete the second loop?
Delete the first. Basically, the inside of the BindToClose function should look exactly like the inside of the PlayerRemoving function
Ok!
I still get the same error
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
Thanks it worked