Hello. I was messing around with my ban script but uh

The error is: attempt to index with a nil number (line 12)

Code:

local bans = game:GetService("DataStoreService"):GetDataStore("TempZens21e45")

game.Players.PlayerAdded:Connect(function(player)
	local banData
	local good, bad = pcall(function()
		banData = bans:GetAsync("plrs_"..tostring(player.UserId))
	end)
	if bad then
		warn("Error getting datastore: "..bad)
	end
	if banData ~= {} then
		local prevtime = banData[1]
		local reason = banData[2]
		local banlength = banData[3]
		if os.time() < (prevtime+banlength) then
			local timeleft = (prevtime+banlength)-os.time()

			local hours = math.floor(timeleft/3600)

			local hs = timeleft % 3600

			local days = math.floor(timeleft/86400)

			local mins = math.floor(hs/60)

			local secsleft = hs%60

			player:Kick(reason.. "\nYou will be unbanned in: "..days.." days, "..hours.." hours, "..mins.." minutes and "..math.floor(secsleft).. " seconds.")
		else
			local g,b = pcall(function()
				bans:SetAsync("plrs_"..tostring(player.UserId),{})
			end)
			if g then
				print("player waited their time and got unbanned succesfully.")
			else
				warn("error setting datastore: ".. b)
				bans:SetAsync("plrs_"..tostring(player.UserId),{})
			end
		end
	end
end)


function BanPlayer(userid,timeinseconds,reason)
	if game.Players:GetPlayerByUserId(userid) then
		local timeleft = timeinseconds

		local hours = math.floor(timeleft/3600)

		local hs = timeleft % 3600
		
		local days = math.floor(timeleft/86400)

		local mins = math.floor(hs/60)

		local secsleft = hs%60
		

		game.Players:GetPlayerByUserId(userid):Kick(reason.. "\nYou will be unbanned in: "..days.." days, "..hours.." hours, "..mins.." minutes and "..math.floor(secsleft).. " seconds.")

		local g,b = pcall(function()
			bans:SetAsync("plrs_"..tostring(userid),{os.time(),reason,timeinseconds})
		end)
		if g then
			print("succesfully banned player.")
		else
			warn("error setting ban datastore: "..b)
			bans:SetAsync("plrs_"..tostring(userid),{os.time(),reason,timeinseconds}) -- attempt agian.
		end
	else
		local g,b = pcall(function()
			bans:SetAsync("plrs_"..tostring(userid),{os.time(),reason,timeinseconds})
		end)
		if g then
			print("succesfully banned player.")
		else
			warn("error setting ban datastore: "..b)
			bans:SetAsync("plrs_"..tostring(userid),{os.time(),reason,timeinseconds}) -- attempt agian.
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if string.lower(msg) == "clown" then
			BanPlayer(plr.UserId,200000,"your the clown")
		end
	end)
end)
1 Like

What value is banData[1] set to in the table originally?

Aha. Try set the values for new players to 0. A new player with no data will always have nil values, unless you set them values

2 Likes

The check for banData should be checking for nil, rather than {} (an empty table).

1 Like

just change line 12 to

if banData ~= {} and banData ~= nil then

or

if banData ~= nil then

like what @Icee444 just said above

1 Like

To expand on that to avoid any confusion: @Fourthbyname_2 is not saying nil and {} (an empty table) are the same thing. If you already have {} in your database, then you’ll need to check for that too. It also depends on that as well as if you want to assign {} values and treat them separately. It’s up to you how you would like to handle that part.

1 Like

Or you can just say if banData then - little bit shorter

2 Likes

how about instead doing

if good then
   (rest of script here)
end
1 Like

Change line twelve to: if banData then

This would check if it exists.

1 Like