Module returning nil in every possible occasion, don't know how to fix it

Hello! I’ve been working on a module that sets a player on a specific rank. The problem is that it returns nil when I try to execute it. I’ve tried 3 variants of the script and none worked. Here’s the last module script (server script down below):


local module = {

    Ranks = {
    
    Special = {
		0; --id

	};
	
	Admins = {

		0; --id

	};
	
	Developers = {
		0; --id
	};
	
	Owner = {
		0; --id
	};

    }

}

local rankInfo = {
	n4 = {Banner = "Owner", NameColor = Color3.fromRGB(255, 0, 221), MessageColor = Color3.fromRGB(255, 233, 62), BannerColor = Color3.fromRGB(14, 215, 255)};
	n3 = {Banner = "Developer", NameColor = Color3.fromRGB(255, 0, 81), MessageColor = Color3.fromRGB(255, 0, 153), BannerColor = Color3.fromRGB(255, 0, 4)};
	n2 = {Banner = "Admin", NameColor = Color3.fromRGB(255, 162, 0), MessageColor = Color3.fromRGB(255, 233, 62), BannerColor = Color3.fromRGB(179, 255, 0)};
	n1 = {Banner = "Special", NameColor = Color3.fromRGB(54, 156, 32), MessageColor = Color3.fromRGB(230, 190, 2), BannerColor = Color3.fromRGB(255, 165, 10)};
	n0 = {Banner = "Visitor", NameColor = Color3.fromRGB(150, 150, 150), MessageColor = Color3.fromRGB(240, 240, 240), BannerColor = Color3.fromRGB(150, 150, 150)}
}

function getRank(id)
	
	local rank = 0
	
	for i, v in ipairs(module.Ranks) do
		
		for _, v2 in pairs(v) do
			
			if id == v2 then
				
				rank = i
				
			end
			
		end
		
	end
	
	return rank
	
end

function module.getName(id)

	return rankInfo['n'..getRank(id)].Banner

end

function module.getMessageColor(id)

	return rankInfo['n'..getRank(id)].MessageColor

end

function module.getTagColor(id)

	return rankInfo['n'..getRank(id)].BannerColor

end

function module.getNameColor(id)

	return rankInfo['n'..getRank(id)].NameColor

end

return module

Server Script:

local ChatService = require(game.ServerScriptService:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))
local Ranks = require(game.ServerStorage.Ranks) -- Ranks Directory

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local Speaker = ChatService:GetSpeaker(Player.Name)
	repeat
		wait()
		Speaker = ChatService:GetSpeaker(Player.Name)
	until Speaker ~= nil or Player == nil
	if Player then
		local Id = Player.UserId
		Speaker:SetExtraData("NameColor", Ranks.getNameColor(Id))
		Speaker:SetExtraData("ChatColor", Ranks.getMessageColor(Id))
		Speaker:SetExtraData("Tags", {{
			TagText = Ranks.getName(Id),
			TagColor = Ranks.getTagColor(Id)
		}})
	end
end)

Any help is appreciated. Thanks!

Hey @Squaddy3,

I did some testing, the problem is that the player is added before your connection starts because you wait for the chat module to start before starting the connection. You can fix this by running your player added code on all existing players and starting the connection for new ones:

local Players = game:GetService("Players")

local ChatService = require(game.ServerScriptService:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))
local Ranks = require(script.ModuleScript) -- Ranks Directory

local function onPlayerAdded(Player)
	local Speaker = ChatService:GetSpeaker(Player.Name)
	if not Speaker then
		repeat
			wait()
			Speaker = ChatService:GetSpeaker(Player.Name)
		until Speaker ~= nil or Player == nil
	end
	if Player then
		local Id = Player.UserId
		Speaker:SetExtraData("NameColor", Ranks.getNameColor(Id))
		Speaker:SetExtraData("ChatColor", Ranks.getMessageColor(Id))
		Speaker:SetExtraData("Tags", {{
			TagText = Ranks.getName(Id),
			TagColor = Ranks.getTagColor(Id)
		}})
	end
end

for _, Player in ipairs(Players:GetChildren()) do
	onPlayerAdded(Player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Your version of the server script works but for some reason it doesn’t give the proper rank value when I enter the game (it sets me as visitor, which is the default rank value). I think the error is in the getRank() function (more specifically the for loops) because when I try to debug it it doesn’t output anything even if I put it outside the if statement.