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

[Not Needed] bad argument #1 to 'pairs' (table expected, got nil)?

I'm getting an error on line 32 that, not sure why or how. I tried changing the DB name, since it's the root of the hierarchy.

Picture https://gyazo.com/86638338ee70f1b6788fe096bcdf1a9b

local AUTO_SAVE = true

local TIME_BETWEEN_SAVES = 60

local PRINT_OUTPUT = true

local SAFE_SAVE = true

  

local players = game:GetService("Players")

local dataStoreService = game:GetService("DataStoreService")

local leaderboardData = dataStoreService:GetDataStore("I8SB90AnsjGJAK?FA*Mz")

  

local function Print(message)

if PRINT_OUTPUT then print(message) end

end

  

local function SaveData(player)

wait(2.5)

if player.userId < 0 then return end

player:WaitForChild("BackendData")

wait()

local BackendData = {}

for i, stat in pairs(player.BackendData:GetChildren()) do

table.insert(BackendData, {stat.Name, stat.Value})

end

leaderboardData:SetAsync(player.userId, BackendData)

Print("Saved "..player.Name.."'s data")

end

  

local function LoadData(player)

if player.userId < 0 then return end

player:WaitForChild("BackendData")

wait()

local leaderboardStats = leaderboardData:GetAsync(player.userId)

for i, stat in pairs(leaderboardStats) do

local currentStat = player.BackendData:FindFirstChild(stat[1])

if not currentStat then return end

currentStat.Value = stat[2]

end

Print("Loaded "..player.Name.."'s data")

end

  

players.PlayerAdded:connect(LoadData)

players.PlayerRemoving:connect(SaveData)

  

if SAFE_SAVE then

game.OnClose = function()

for i, player in pairs(players:GetChildren()) do

SaveData(player)

end

wait(1)

end

end

  

while AUTO_SAVE do

wait(TIME_BETWEEN_SAVES)

for i, player in pairs(players:GetChildren()) do

SaveData(player)

end

end

8 comments

0

What exactly is BackendData?

0

It's an IntValue inside of the player that stores the Money, JailTime, and if the player owns the car or not. Then inside the player we have leaderstats that holds Jail Time and Money and gets the values from BackendData, it's data that can't be edited besides the admin panel. If you need a clarification I can give one.

0

How can whitespace cause you this error?

0

good luck

0

@PhantomVisual your answer is wrong in so many ways

0

@incapaxx My mistake. Didn't even test it in-studio, just assumed. Thanks for telling me!

0

Idk, it still works. It just doesn't save when the player leaves, only saves every 60 seconds. It's fine. I put a disclaimer every 60 seconds to wait every 60 seconds for their data to save.

0

Sorry, but I'm closing this question. The line that you claim to be causing you issues is just whitespace, which means you did not post full code, or you made changes to the code that affected the line count, and there are multiple occurrences of you calling the pairs function; which call to pairs causes your problem?

1 answer

Trial 2 (I messed up hard with my first answer lol)

I believe when the last player leaves, it tries to save the player's data. But the player is 'deleted' because the server closes before it can even save it.

Once OnClose is fired, there are zero players in the game. It will pass the loop and wait 1 second before closing. While that is happening, the SaveData() function is waiting 2.5 seconds before saving the player's data but the server will be closed by then.

I hope this is correct this time...

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