Random error all of the sudden?

  1. What do you want to achieve?
    I just wanna detect if a bool is true.

  2. What is the issue?
    I’m getting a error saying that the table that I’m getting the bool from is well nil.

  3. What solutions have you tried so far?
    I’ve looked around the devForums a bit.

The script worked fine for days but all of the sudden, it stopped working

ServerStorage.SimpliCmd.Modules.Functions:18: attempt to index nil with ‘banned’ - Server - Functions:18

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("SimpliCmdBans10")
local SS = game:GetService("ServerStorage")
local cmds = require(script.Parent.Commands)

local module = {

	CheckBan = function(plr)

		local Id = plr.UserId

		local banData
		local success, warnd = pcall(function()
			banData = DS:GetAsync(Id)
		end)

		if success then
			if banData.banned then -- this is line 18
				
				if banData.endTime == "Permanent" then
					plr:Kick("\n Banned Permanently".."\n Reason: "..banData.reason)
					return
				end
				
				if banData.endTime - os.time() <= 0 then
					cmds.UnBan(plr)
					print("Player unbanned")
					return
				end
				
				local now = os.date("!*t", banData.endTime) -- "!*t" just means it's utc rather than local
				local formatter = "%02i" -- formatter:format() will coerce a number to be a two-digit zero-padded str
				-- for example, formatter:format(2) --> 02
				local unBanTime = (formatter:format(now.month) .. "/" .. formatter:format(now.day) .. "/" .. now.year)
				
				plr:Kick("\n Banned".."\n Reason: "..banData.reason.."\n Unban Date: \n"..unBanTime)
			end
		end
		

	end,

}

return module
1 Like

You have to make sure data is actually loaded into the banData variable. Your script assumes that something is assigned to the variable, but DataStore::GetAsync can return nil:

if success and banData then -- make sure the banData variable also has a value
			if banData.banned then

success just means the function ran without any problems, it doesn’t guarantee that it’s going to return something

3 Likes

That makes sense I guess, I also implemented this:

repeat 
			local success, warnd = pcall(function()
				banData = DS:GetAsync(Id)
			end)
		until success and banData

which should try getting the info until success if I scripted this right?
Thanks by the way.

1 Like

Yes, but you should also be mindful of DataStore Limits. So if it fails to load data after a certain amount of retries, you can assign a default value (if necessary)

1 Like

Not sure why I thought Getting Async wouldn’t use limits, I’ll keep this in mind.