For loop is skipping half the table

Ok I have this continuing problem, this script on character added runs this
image

Where rank,picture,team,sequence is suppose to return on of these
image
And the problem im having is it completely skips part of the table and i have no idea why.
It skips these

If you have any ideas please respond as soon as possible, its delaying an update, I found it weird because i’ve done these things hundreded of times and its always worked

Also, here is the script


local module = {}

ReplicatedStorage = game:GetService("ReplicatedStorage")

ClientModules = ReplicatedStorage.Modules

ReplicatedStorage = game:GetService("ReplicatedStorage")

ClientModules = ReplicatedStorage.Modules

Overhead = {
	["Simple"] = {
		group = 9116478,
		minrank = 255,
		userid = {0},
		picture = 0,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["MIT"] = {
		group = 0,
		minrank = 1,
		userid = {315297322},
		picture = 5253970,
		sequence = ColorSequence.new(Color3.new(0.623529, 0.0196078, 0.666667),Color3.new(1, 0, 1))
	},
	["Southern"] = {
		group = 9116478,
		minrank = 254,
		userid = {0},
		picture = 0,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},

	["Aiden"] = {
		group = 9116478,
		minrank = 253,
		userid = {189142313,81718700},
		picture = 4555772172,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["AHOD - Low"] = {
		group = 9116478,
		minrank = 252,
		userid = {81718700},
		picture = 0,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["Management"] = {
		group = 9116478,
		minrank = 225,
		userid = {0},
		picture = 0,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	
	["Developer"] = {
		group = 9116478,
		minrank = 218,
		userid = {0},
		picture = 0,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["Head of Staff"] = {
		group = 9116478,
		minrank = 208,
		userid = {0},
		picture = 318791109,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["Senior Admin"] = {
		group = 9116478,
		minrank = 205,
		userid = {0},
		picture = 318791109,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["Admin"] = {
		group = 9116478,
		minrank = 204,
		userid = {0},
		picture = 318791109,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["Senior Mod"] = {
		group = 9116478,
		minrank = 203,
		userid = {0},
		picture = 318791109,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["Mod"] = {
		group = 9116478,
		minrank = 201,
		userid = {318791109},
		picture = 318791109,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["TMod"] = {
		group = 9116478,
		minrank = 200,
		userid = {0},
		picture = 318791109,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	},
	["VIP"] = {
		group = 9116478,
		minrank = 24,
		userid = {0},
		picture = 0,
		sequence = ColorSequence.new(Color3.new(0.109804, 0.639216, 0.666667),Color3.new(0.164706, 1, 0.541176))
	}
}


totalnum = 0

function initcheck()
	for i,v in pairs(Overhead) do
		totalnum += 1
	end
end
initcheck()

function module.RequestRank(plr)
	local pid = plr.UserId
	local num = 0
	for i,v in pairs(Overhead) do
		print(i)
		print(v)
		num += 1
		if plr:GetRankInGroup(v.group) >= v.minrank then
			print(i)
			return i,v.picture,plr.Team.Name,v.sequence		
		else
			if table.find(v.userid,pid) then
				return i,v.picture,plr.Team.Name,v.sequence		
			end
		end
		wait(0.005)
	end
	if num == totalnum then
		return nil,nil,plr.Team.Name,nil
	end
end


function module.Run(plr)
	print("Hooked Player")
	local char = plr.Character
		local rank,picture,team,sequence = module.RequestRank(plr)
		local GUI = script.Rank:Clone()
		GUI.User.Text = plr.Name
		GUI.Team.Text = team
		GUI.Team.TextColor3 = plr.TeamColor.Color

		if picture ~= nil then
			GUI.Special.RANKIMAGE.Image = "rbxassetid://"..picture
			GUI.Rank.Text = rank
			GUI.Special.Gr.Color = sequence
		else
			GUI.Special:Destroy()
			GUI.Rank:Destroy()
		end

		GUI.Parent = char.Head
end



return module

1 Like

If you have a return, the loop will stop and the function will end. You might want to add the results to a table and then return the table after the loop is finished.

1 Like

The point of it is to stop the script when it returns to prevent going through the entire table. and just stop it at that point. Its also not that either, it just straight up skipping half the table, doesnt print or anything.

If you are using a dictionary like you are

["Simple"] =,
["MIT"] =,
etc`

instead of an array

{name = "Simple", group = 9116478, etc},
{name = "MIT", group = etc}

then they don’t actually have an order. However you type it out won’t be how the script interprets it. It can’t be predicted or relied upon. You will need to make in an array.

This has to do with the order of the array and returning. If you loop through Overhead normally and print each “i” variable, you’ll see that it isn’t the same order as it appears on your screen. Say I am rank 203 and the order of the array is something like:

["Simple"] = {
	minrank = 255,
},
["Developer"] = {
	minrank = 218,
},
["MIT"] = {
	minrank = 1,
},
["Mod"] = {
	minrank = 201,
}

Once I reach MIT, the script returns and the loop stops. This is what is happening to you but your randomized table is probably something like:

{
    "Simple",
    "Developer",
}

Here is a script that helps visualize it:

local rank = 203

function module.RequestRank(plr)
	local pid = plr.UserId
	local num = 0
	
	for i,v in pairs(Overhead) do
		print(i)
	end
	print("---")
	for i,v in pairs(Overhead) do
		num += 1
		if rank >= v.minrank then
			print(1)
			return i
		else
			if table.find(v.userid,pid) then
				print(2)
				return i	
			end
		end
	end
	
	if num == totalnum then
		print(3)
		return "None"
	end
end

--[[
print(Module.RequestRank(player))
]]

To simplify, it’s not actually skipping but “Simple” and “Developer” are most likely the first 2 items in the dictionary. Also, the userid thing might also interfere but I’m not sure what it does.

1 Like