Help with 'for _, in pairs'

I keep getting this error when using ‘for _, in pairs’. I am using JSONDecode to get the data from my website, also I am new to using “for do”

invalid argument #1 to 'pairs' (table expected, got Instance)

Here is the part of the code that the issue is coming from.

local HttpServ = game:GetService("HttpService")
local WebsiteResponse = HttpServ:GetAsync("http://toddhow.xyz/v.json")
local banData = HttpServ:JSONDecode(WebsiteResponse)

local scriptBans = {
	'2533418'
}

function checkBans(plr)
	local bans = banData.groupId
	for _,bans in pairs(scriptBans) do

Try structuring it correctly, use this as a reference but you should structure it as a dictionary:

local scriptBans = {
[1] = “000”
}

^ This way it knows the index

I would try that

1 Like

Please call this a generic for loop.

You aren’t really telling us what you want to do within the iteration or what to use it for, so we can’t assume what’s wrong other than a pointless loop with no code within the block, just put an end after this

to complete it.

If you just need to learn about it then visit these links:

https://developer.roblox.com/en-us/articles/Loops
https://www.lua.org/pil/4.3.4.html
https://www.lua.org/pil/4.3.5.html

Also, the error couldn’t have come from here since you’re attempting to iterate through a table in this sample - you’re trying to loop through an instance somewhere.

4 Likes

Also if your looking for a UserId or GroupId change it from a String to an Int64

1 Like

This sample snippet below works.

local scriptBans = {
	'2533418'
}
for _,bans in pairs(scriptBans) do 
print(bans) 
end

Do you reference/define scriptBans anywhere else in your script?

2 Likes

scriptBans is being seen as an Instance and not the table as you expect, the code should work fine so then something could have replaced your scriptBans variable.

2 Likes

Ok thank you, I will try that when I can get back to my project.

1 Like

When I am able to get to my computer I will send the full script

Here is the full copy of the script.

local HttpServ = game:GetService("HttpService")
local WebsiteResponse = HttpServ:GetAsync("http://toddhow.xyz/v.json")
local banData = HttpServ:JSONDecode(WebsiteResponse)

local bans = {
	'2533418'
}

function checkBans(plr)
	local bans = banData.groupId
	for _,bans in pairs(bans) do
		local GroupServ = game:GetService("GroupService")
		
		local groupInfo = GroupServ:GetGroupInfoAsync(banData.groupId)
		
	if plr:IsInGroup(banData.groupId) then
			plr:Kick("n\You are in a group that has been indefinitely banned by the Shake Shack Association.\n\nGroup: "..groupInfo.name.."n\n\Group Id: "..groupInfo.Id)
   		end
   	end
end

game.Players.PlayerAdded:Connect(checkBans)

for i,v in pairs(banData.groupId) do
	checkBans()
end

you’re redefining bans right here, name this variable something else or change your other bans variable

1 Like

I would recommend defining the GroupService at the top of your script just like HttpService.

1 Like

I have figured out the issue with the for loop but I can’t get the loop to work with JSONdecode

Did you read what presssssure said in their reply?

Yes, I should do a test to see if it kicks a alt and not my main.