Role giving scripts have a bug

  1. I’m trying to make a role system that gives a maximum amount of one role to people
  2. It is picking multiple roles for a single person and sometimes going past the maximum role being given out.
  3. I looked on dev forum but not many people have had the same problem as I have had.

Here is all the code affiliated with the role system:
Module:

local CardModule = {}




CardModule.MaximumRoles = {

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

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

	["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
					print("Not returning...")
		end
		end
		end
	


return CardModule

Local script that gives everyone roles:

game.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 RoleNarraration remote 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)


GameStart script:

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

It works when one player joins the game but when multiple people join:

Please reply if you know the answer, I have reposted this 3 times and all of them haven’t gotten a response.

I think one issue and reason why no one can respond is because theres not enough info given, personally this is alot to take in with not much context on where and what the intended outcome is, if you can re-explain that would be dope :grinning_face_with_smiling_eyes:

1 Like

The intended outcome is that there should be a limit to a role being given out.
For example: If the max limit of werewolf cards is 2 then when 2 players are werewolves you will no longer be able to get the werewolf card.

Have I explained it better?

Can anybody help me? Please tell me any other things you may not understand.