My ban datastore script keeps having the same error "invalid key to 'next'"

Whenever I load up my game, my ban datastore has an error “invalid key to ‘next’”. I have tried investigating the cause of this but I have no luck every time I try. Can anyone help me diagnose the problem and fix it in my script? Thanks for reading this!

Error:

Script:

local table1 = {42310359}
local data = game:GetService("DataStoreService"):GetDataStore("BannedUsers");
local banmessages = {"Server-Banned."}

while wait() do
	table1 = {} -- clearing table
	for _,bannedusers in next, data:GetAsync("bans") do
		table.insert(table1,bannedusers)
	end
	for _,bannedusers in next, game.ReplicatedStorage.ServerbannedPlayers:GetChildren() do
		table.insert(table1,bannedusers.Value)
	end
	for _,player in next, game.Players:GetPlayers() do
		for _,bannedplayer in next, table1 do
			if typeof(bannedplayer) == "number" then
				if player.UserId == bannedplayer then
					player:Kick(banmessages[math.random(1,#banmessages)]) -- Get WI-FI Anywhere you go. Hold up bgfufafagjgafegaerta
				end
			elseif typeof(bannedplayer) == "table" then
				if player.UserId == bannedplayer.USER_ID then
					local timetillunban = "Unknown"
					if bannedplayer.TIME == "Permament" then
						timetillunban = "Never"
					end
					player:Kick("Banned, Reason: "..bannedplayer.REASON.." Time banned: "..bannedplayer.TIMEBANNED.." Time till unban: "..bannedplayer.TIME.." ("..timetillunban..")") -- Get WI-FI Anywhere you go. Hold up bgfufafagjgafegaerta
				end
			end
		end
	end
end
1 Like

Could data:GetAsync("bans") be returning nil? Also I think for the sake of clarity you sohuld be using pairs or ipairs instead

1 Like

Good idea! However when I tried changing it to pairs I get another error message

What does data:GetAsync("bans") return?

Also, when changing to pairs, did you just change next to pairs or did you remeber to put the getasync into brackets

I just changed the next to pairs.

data:GetAsync("bans")

returns all of the banned players that get banned from my ban command.

Changing it to pairs isn’t enough, you also need to change how your call the pairs function,

next, someTable

would need to be changed to

pairs(someTable)

If that still doesn’t work it’s likely something wrong with the bans key

2 Likes

Thank you so much! I always appreciate it when you respond to my posts to help me out.

1 Like