"Stack overflow" error in module script

I am trying to make a role system that has a limit to a role being given out.
When people play the game and a role has reached its limit, it goes past the limit anyway.
It also shows this error:

Here is the module script:

local CardModule = {}




CardModule.MaximumRoles = {

	["Werewolf"] = {
		["MaximumRole"] = 2;
	};

	["Villager"] = {
		["MaximumRole"] = 1;
	};

	["Hunter"] = {
		["MaximumRole"] = 1;
	};

	["Tanner"] = {
		["MaximumRole"] = 1;
	};

	["Seer"] = {
		["MaximumRole"] = 1;
	};

	["Minion"] = {
		["MaximumRole"] = 1;
	};

	["Drunk"] = {
		["MaximumRole"] = 1;
	};

	["Mason"] = {
		["MaximumRole"] = 2;
	};
}




CardModule.ChooseRandomCard = function()
			for i,v in pairs(game.Players:GetPlayers()) do
				if v:FindFirstChildWhichIsA("IntValue").Value < 1 then
			for i,v in pairs(game.ReplicatedStorage.RoleValues:GetChildren()) do
				if v.Value < CardModule.MaximumRoles[v.Name].MaximumRole then
					
					print(v.Name)
					return v.Name
				end
					end
				else
					CardModule.ChooseRandomCard()
		end
		end
		end
	


return CardModule

Local script that requires the module script:

ame.ReplicatedStorage.RoleNarraration.OnClientEvent:Connect(function()
	local cardModule = require(game.ReplicatedStorage.ModuleScript)
	local card = cardModule.ChooseRandomCard()
	game.ReplicatedStorage.Role:FireServer()
	game.ReplicatedStorage.Role3:FireServer(card)
	
game.ReplicatedStorage.Role.OnClientEvent:Connect(function(value)
		
	
	

	
	game.ReplicatedStorage.RoleValues:FindFirstChild(value).Value += 1
	script.Parent.Parent.Exit.Visible = true
		
		game.ReplicatedStorage.BillboardGuie.OnClientEvent:Connect(function(text)
			local clone = game.ReplicatedStorage.BillboardGui:Clone()
			clone.Parent = game.Players.LocalPlayer.Character:WaitForChild("Head")
		game.Players.LocalPlayer.Character.Head:WaitForChild("BillboardGui").TextLabel.Text = text
end)
	end)
end)

Script that fires the RoleNarraration event:

game.Players.PlayerAdded:Connect(function(player)
		
	local villager = Instance.new("IntValue")
	villager.Name = "Villager"
	villager.Parent = player
	local werewolf = Instance.new("IntValue")
	werewolf.Parent = player
	werewolf.Name = "Werewolf"
	local tanner = Instance.new("IntValue")
	tanner.Parent = player
	tanner.Name = "Tanner"
	local mason = Instance.new("IntValue")
	mason.Parent = player
	mason.Name = "Mason"
	local seer = Instance.new("IntValue")
	seer.Parent = player
	seer.Name = "Seer"
	local hunter = Instance.new("IntValue")
	hunter.Parent = player
	hunter.Name =  "Hunter"
	local minion = Instance.new("IntValue")
	minion.Parent = player
	minion.Name = "Minion"
	local drunk = Instance.new("IntValue")
	drunk.Parent = player
	drunk.Name = "Drunk"		
	end)

game.ReplicatedStorage.GameStart.OnServerEvent:Connect(function()
	wait(2)
	game.ReplicatedStorage.RoleNarraration:FireAllClients()
	game.ReplicatedStorage.Role.OnServerEvent:Connect(function()
		wait(1)
		for i,player in pairs(game.Players:GetPlayers()) do
			
			for i,intvalue in pairs(player:GetChildren()) do
				if intvalue.ClassName == "IntValue" then
					if intvalue.Value > 0 then
	
			for i,v in pairs(player:GetChildren()) do
				
				if v.ClassName == "IntValue" then
					
								if v.Value < 1 then
									local int = intvalue.Name
									game.ReplicatedStorage.Role:FireAllClients(intvalue.Name)
									player:WaitForChild("PlayerGui").MainGui.Narraration.Text = "Your role is "..int.." (Click the information button for more info.)"
									game.ReplicatedStorage.BillboardGuie:FireAllClients(" "..int.." (Only you can see this)")
						
						v:Destroy()
							end
						end
						end
				end
			end
			end
		end
		end)
	end)



Local script that fires GameStart event:

if not game:IsLoaded() then
	game.Loaded:Wait()
script.Parent.Visible = true
else
	ReplicatedFirst:RemoveDefaultLoadingScreen()
	screenGui:Destroy()
	game.ReplicatedStorage.GameStart:FireServer()
end




If you have any unanswered questions please ask me them.

1 Like

The problem might be because you are calling the function inside of a for loop that is inside of that function. Looking at your code that’s the only thing that I think could trigger something like that.

1 Like

Should I put nothing there instead or would that return the value’s name?

1 Like

Your function keeps calling itself.

What would I do to not return the value’s name in the else statement? Would I put nothing in the else statement?

If you want to return nothing, it’s return nil.
However, it seems like you would want to just remove the else statement.
It will continue looping over the players until it returns something, and if it returns nothing it will implicitly return nil.

I never realized it was that obvious. Going to test.

So far there are no errors. Should I mark this as a solution for now and if it has errors in the future should I unmark the solution?