Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I am trying to code a role picker. Can anybody help?

Asked by 3 years ago

So basically, I am trying to code a role picker. There is one person chosen to be a werewolf, one person chosen to be a villager, one person chosen to be a clairvoyant, one person chosen to be a witch, one person chosen to be a jester and one person chosen to be a clown. I also want to make it so that there is only one person per role, no one can have the same role as the other. Everything in this script works except for the role picker (at the bottom of the script). Can anybody help me? Here is the whole script that I have made:

--Variables
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local Status = game.ReplicatedStorage.Status
--Script
while true do

    Status.Value = "Waiting for enough players to play"
    repeat wait(1) until game.Players.NumPlayers >= 2
    Status.Value = "Intermission"
    wait(30)
    Status.Value = "Get ready!"
    wait(4)

    local plrs = {}

    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            table.insert(plrs,player) -- Add each player into plrs table
        end
    end

    local GameZone = game.Workspace.GameZone

    local ClonedGameZone = GameZone:Clone()
    ClonedGameZone.Parent = workspace

    --Teleporting players to the gamezone
    local Seats = ClonedGameZone:FindFirstChild("Seats")
    if not Seats then
        print("Seats not found")
    end

    local AvailableSeats = Seats:GetChildren()
    for i, player in pairs(plrs) do
        if player then
            character = player.Character

            if character then
                -- Teleport them

                character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSeats[1].CFrame + Vector3.new(0,10,0)
                table.remove(AvailableSeats,1)

                character.Humanoid.WalkSpeed = 0
                character.Humanoid.JumpPower = 0

                local GameTag = Instance.new("BoolValue")
                GameTag.Name = "GameTag"
                GameTag.Parent = player.Character   
            else
                -- There is no character
                if not player then
                    table.remove(plrs,i)
                end
            end
        end
    end
    wait(4)
    if #plrs == 2 then
        Status.Value = "Choosing roles.."
        wait(1)

        local Enemies = {}
        local Allies = {}
        local Neutrals = {}

        local WerewolfNumber = math.random(1, #plrs)
        local Werewolf = #plrs[WerewolfNumber]
        table.remove(plrs,WerewolfNumber)
        WerewolfNumber.PlayerGui.MainFrame.Visible = true
        WerewolfNumber.PlayerGui.MainFrame.Role.Text = "A WEREWOLF"
        WerewolfNumber.PlayerGui.MainFrame.Role.TextColor3 = Color3.fromRGB(203, 0, 4)
        WerewolfNumber.PlayerGui.MainFrame.Description.Text = "You are an enemy. You can kill anyone except your teammate: the Clown. Blend in with the allies to not look suspicious, try not to get voted out and win by killing all allies until there is one left. Good luck."
        table.insert(Enemies,WerewolfNumber)

        local ClownNumber = math.random(1, #plrs)
        local Clown = #plrs[ClownNumber]
        table.remove(plrs,ClownNumber)
        ClownNumber.PlayerGui.MainFrame.Visible = true
        ClownNumber.PlayerGui.MainFrame.Role.Text = "A CLOWN"
        ClownNumber.PlayerGui.MainFrame.Role.TextColor3 = Color3.fromRGB(253, 102, 8)
        ClownNumber.PlayerGui.MainFrame.Description.Text = "You are an enemy. Only once in the whole game, you can kill anyone, except for your teammate: the Werewolf, when it's their turn to play. Blend in with the allies to not look suspicious, try not to get voted out and win by killing all allies until there is one left. Good luck."
        table.insert(Enemies,ClownNumber)

        local VillagerNumber = math.random(1, #plrs)
        local Villager = #plrs[VillagerNumber]
        table.remove(plrs,VillagerNumber)
        VillagerNumber.PlayerGui.MainFrame.Visible = true
        VillagerNumber.PlayerGui.MainFrame.Role.Text = "A VILLAGER"
        VillagerNumber.PlayerGui.MainFrame.Role.TextColor3 = Color3.fromRGB(206, 205, 134)
        VillagerNumber.PlayerGui.MainFrame.Description.Text = "You are an ally. You do not have a turn to play during the night so you cannot do anything. However, your goal is to try to vote out the enemies (the Werewolf and the Clown). Good luck."
        table.insert(Allies,VillagerNumber)

        local JesterNumber = math.random(1, #plrs)
        local Jester = #plrs[JesterNumber]
        table.remove(plrs,JesterNumber)
        JesterNumber.PlayerGui.MainFrame.Visible = true
        JesterNumber.PlayerGui.MainFrame.Role.Text = "A JESTER"
        JesterNumber.PlayerGui.MainFrame.Role.TextColor3 = Color3.fromRGB(4, 61, 206)
        JesterNumber.PlayerGui.MainFrame.Description.Text = "You are neutral. Your goal is to get voted out. However, you don't win when you get killed. Act suspicious and blend in with the enemies. Good luck."
        table.insert(Neutrals,JesterNumber)

        local WitchNumber = math.random(1, #plrs)
        local Witch = #plrs[WitchNumber]
        table.remove(plrs,WitchNumber)
        WitchNumber.PlayerGui.MainFrame.Visible = true
        WitchNumber.PlayerGui.MainFrame.Role.Text = "A WITCH"
        WitchNumber.PlayerGui.MainFrame.Role.TextColor3 = Color3.fromRGB(80, 0, 206)
        WitchNumber.PlayerGui.MainFrame.Description.Text = "You are an ally. After the werewolves have killed someone, you can either revive the victim with your reviving potion that you can use only once in the entire game, kill someone other than the victim with your killing potion that you can use only once in the entire game, or not do anything. Try to vote out the enemies. Good luck."
        table.insert(Allies,WitchNumber)

        local ClairvoyantNumber = math.random(1, #plrs)
        local Clairvoyant = #plrs[ClairvoyantNumber]
        table.remove(plrs,ClairvoyantNumber)
        ClairvoyantNumber.PlayerGui.MainFrame.Visible = true
        ClairvoyantNumber.PlayerGui.MainFrame.Role.Text = "A CLAIRVOYANT"
        ClairvoyantNumber.PlayerGui.MainFrame.Role.TextColor3 = Color3.fromRGB(252, 0, 215)
        ClairvoyantNumber.PlayerGui.MainFrame.Description.Text = "You are an ally. When it's your turn to play during the night, you can choose someone to see their role, so choose wisely. Try to vote out the enemies. Good luck."
        table.insert(Allies,ClairvoyantNumber)
    end

end

2 answers

Log in to vote
1
Answered by 3 years ago
local WerewolfNumber = math.random(1, #plrs)
local Werewolf = #plrs[WerewolfNumber]
table.remove(plrs,WerewolfNumber)
WerewolfNumber.PlayerGui.MainFrame.Visible = true -- Number is a number!
WerewolfNumber.PlayerGui.MainFrame.Role.Text = "A WEREWOLF"
....

should be

local WerewolfNumber = math.random(1, #plrs)
local Werewolf = #plrs[WerewolfNumber]
table.remove(plrs,WerewolfNumber)
Werewolf.PlayerGui.MainFrame.Visible = true -- Now we are referencing the player object
Werewolf.PlayerGui.MainFrame.Role.Text = "A WEREWOLF"
Ad
Log in to vote
0
Answered by 3 years ago

(This is a comment)

Some tips:

  • Open the Output window if you haven't already so that you can see what errors occur
  • It's bad practice to update the UI on the server (though it'll work okay in this case). You can use RemoteEvents to avoid this.
  • You have a lot of duplicate code (ie code that is very similar). If you learn how to use tables/dictionaries (and/or functions), you'll be able to reduce this duplication.

ex:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Status = game.ReplicatedStorage.Status

local Roles = { -- List of roles in order to hand them out
    {
        RoleText = "A WEREWOLF",
        TextColor3 = Color3.fromRGB(203, 0, 4),
        Description = "You are an enemy. You can kill anyone except your teammate: the Clown. Blend in with the allies to not look suspicious, try not to get voted out and win by killing all allies until there is one left. Good luck.",
        Team = "Enemies",
    },
    {
        RoleText = "A VILLAGER",
        TextColor3 = Color3.fromRGB(206, 205, 134),
        Description = "You are an ally. You do not have a turn to play during the night so you cannot do anything. However, your goal is to try to vote out the enemies (the Werewolf and the Clown). Good luck.",
        Team = "Allies",
    },
    -- TODO repeat for each other role
}

while true do
    -- ... (TODO I removed the code above the role-giving-out-section for clarity, but you should put it back)
    wait(4)
    if #plrs == 2 then
        Status.Value = "Choosing roles.."
        wait(1) -- NOTE: You don't have to wait here, it just makes everything take longer
        local teams = {
            Enemies = {},
            Allies = {},
            Neutrals = {},
        }
        for _, role in ipairs(Roles)
            local num = math.random(1, #plrs)
            local player = table.remove(plrs, num)
            local MainFrame = player.PlayerGui.MainFrame
            MainFrame.Visible = true
            MainFrame.Role.Text = role.RoleText
            MainFrame.Role.TextColor3 = role.TextColor3
            MainFrame.Description.Text = role.Description
            table.insert(teams[role.Team], player)
            if #plrs == 0 then -- no more players; stop giving out roles
                break
            end
        end
        -- if you want your Enemies/etc variables, you can do this:
        local Enemies = teams.Enemies
        local Allies = teams.Allies
        local Neutrals = teams.Neutrals
        -- and then use them here
        -- of course, you could also just use teams.Enemies or teams["Enemies"]
    end
end

Notice how much less code that last if statement takes up? This lets you change how it works in one place rather than several. Also, you can now easily change the order of roles, or add new roles.

It may be beneficial to put those roles in a ModuleScript so that other scripts can use them.

Answer this question