I have this script that is supposed to assign every player to a random array/table. The first three players should be assigned to the "Werewolves" table, while the rest is completely random. Why is no player being assigned to any role? (Note: The script did assign the players randomly and it worked before adding that "if Werewolfcount >=") Dont get intimidated by the size, its pretty much the same script repeated and slightly changed:
local Players = game:GetService("Players") local Roles = game.ReplicatedStorage.roles:GetChildren() Villagers = {} Werewolves ={} Arsonists = {} Hunters ={} Mediums ={} Seers ={} Jesters ={} Witches ={} Minimumwolves = 3 Werewolvescount = table.getn(Werewolves) MaximumSeers = 1 MaximumArsonists = 1 MaximumJesters = 1 game.Players.PlayerAdded:Connect(function(plr) local NeedsRole = Instance.new("BoolValue") NeedsRole.Name = ("NeedsRole") NeedsRole.Parent = plr NeedsRole.Value = true print("Just joined: "..plr.Name) if table.getn(Players:GetChildren()) == 4 then print("There are "..table.getn(Players:GetChildren()).." Players, choosing Roles!") wait(1) print("...Choosing...") wait(3) for _,player in pairs(Players:GetChildren()) do local ChooseTable = math.random(1,8) player.NeedsRole.Value = true if ChooseTable == 1 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Villagers,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 2 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Werewolves,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 3 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Arsonists,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 4 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Hunters,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 5 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Mediums,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 6 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Seers,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 7 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Jesters,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end elseif ChooseTable == 8 and Werewolvescount >= 3 then if player.NeedsRole.Value == true then table.insert(Witches,1,player) player.NeedsRole.Value = false else if Werewolvescount <3 then table.insert(Werewolves,1,player) end print(player.Name.." already has a role!") end end end print("") print("ROLETABLES AFTER ASSIGNMENT!:") print("") print("Villagers: ") print(#Villagers) wait(1) print("Werewolves: ") print(#Werewolves) wait(1) print("Arsonists: ") print(#Arsonists) wait(1) print("Hunters: ") print(#Hunters) wait(1) print("Mediums: ") print(#Mediums) wait(1) print("Seers: ") print(#Seers) wait(1) print("Jesters: ") print(#Jesters) wait(1) print("Witches: ") print(#Witches) else print("Not enough players!") end end)
Hello, KochbananemitGuave! Thanks for providing sufficient work and logic to render a respectable question.
The main problem that I see so far is that you have made the assumption that the variable Werewolvescount
keeps track/updates every time that you add a player to the table Werewolves
Werewolvescount = table.getn(Werewolves)
table.getn(Werewolves)
will return the size of its argument (table) as a numerical value. Since you have set the variable equal to table.getn(Werewolves)
you have therefore set the variable equal to the value that the function RETURNS. NOT the function's action itself.
What you are actually trying to do is this:
local function Werewolvescount() return table.getn(Werewolves) end
Now every spot where you reference Werewolvescount
you should replace with Werewolvescount()
. This will return a properly updated size of the table each time you need to compare its size through your If Statements
Note: This is not the cleanest or most efficient method to complete the task you are trying to write this script to achieve - I am only solving the current issue with the code. If you're interested in rewriting feel free to DM me.
I hope this helps! Let me know!