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 4 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:

001--Variables
002local ReplicatedStorage = game:GetService("ReplicatedStorage")
003 
004local ServerStorage = game:GetService("ServerStorage")
005 
006local Status = game.ReplicatedStorage.Status
007--Script
008while true do
009 
010    Status.Value = "Waiting for enough players to play"
011    repeat wait(1) until game.Players.NumPlayers >= 2
012    Status.Value = "Intermission"
013    wait(30)
014    Status.Value = "Get ready!"
015    wait(4)
View all 125 lines...

2 answers

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

should be

1local WerewolfNumber = math.random(1, #plrs)
2local Werewolf = #plrs[WerewolfNumber]
3table.remove(plrs,WerewolfNumber)
4Werewolf.PlayerGui.MainFrame.Visible = true -- Now we are referencing the player object
5Werewolf.PlayerGui.MainFrame.Role.Text = "A WEREWOLF"
Ad
Log in to vote
0
Answered by 4 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:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local ServerStorage = game:GetService("ServerStorage")
03local Status = game.ReplicatedStorage.Status
04 
05local Roles = { -- List of roles in order to hand them out
06    {
07        RoleText = "A WEREWOLF",
08        TextColor3 = Color3.fromRGB(203, 0, 4),
09        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.",
10        Team = "Enemies",
11    },
12    {
13        RoleText = "A VILLAGER",
14        TextColor3 = Color3.fromRGB(206, 205, 134),
15        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.",
View all 52 lines...

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