- Im trying to make a role system which gives a certain number of a role to multiple people
- It is picking multiple roles for a single person and sometimes going past the maximum role being given out.
- I have tried so many things but they aren’t working.
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.