When looping through players, consider adding a coroutine if you're waiting for a specific period of time.
A coroutine creates a new thread & allows the environment to continue running without yielding; this is what we call Multithreading.
01 | local PlayersService = game:GetService( "Players" ) |
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 | local ServerStorage = game:GetService( "ServerStorage" ) |
05 | local BoolValue = ReplicatedStorage:WaitForChild( "inRound" ) |
06 | local Knife = ServerStorage:WaitForChild( "Knife" ) |
09 | ReplicatedStorage:WaitForChild( "inRound" ).Changed:Connect( function (property) |
10 | if tostring (property) = = "Value" then |
11 | local inRound = BoolValue.Value |
12 | if inRound = = true then |
13 | local Chosen = PlayersService:GetPlayers() [ math.random( 1 , #PlayersService:GetPlayers()) ] |
14 | local PlayerGui = Chosen:FindFirstChildWhichIsA( "PlayerGui" ) or Chosen:WaitForChild( "PlayerGui" ) |
16 | local Picker = PlayerGui:WaitForChild( "Picker" ) |
17 | local Background = Picker:WaitForChild( "Background" ) |
18 | local RoleGiven = Background:WaitForChild( "RoleGiven" ) |
20 | RoleGiven.Text = "Murderer" |
21 | RoleGiven.TextColor 3 = Color 3. fromRGB( 255 , 64 , 64 ) |
23 | coroutine.wrap( function () |
25 | Background.Visible = false |
27 | local BackpackItem = Knife:Clone() |
28 | BackpackItem.Name = "Knife" |
29 | BackpackItem.Parent = Chosen:FindFirstChildWhichIsA( "Backpack" ) or Chosen:WaitForChild( "Backpack" ) |
32 | pcall ( print , Chosen:GetFullName() .. " is now the suspected killer." ) |
34 | for _, Player in pairs (PlayersService:GetPlayers()) do |
35 | if Player ~ = Chosen and Player.Character and typeof(Player.Character) = = "Instance" and Player.Character:IsDescendantOf(workspace) then |
36 | local Humanoid = Player.Character:FindFirstChildWhichIsA( "Humanoid" ) |
37 | if Humanoid:GetState() ~ = Enum.HumanoidStateType.Dead and Humanoid.Health > 0 then |
38 | table.insert(Survivors, Player) |
39 | local PlayerGui = Player:FindFirstChildWhichIsA( "PlayerGui" ) or Player:WaitForChild( "PlayerGui" ) |
41 | local Picker = PlayerGui:WaitForChild( "Picker" ) |
42 | local Background = Picker:WaitForChild( "Background" ) |
43 | local RoleGiven = Background:WaitForChild( "RoleGiven" ) |
45 | RoleGiven.Text = "Victim" |
46 | RoleGiven.TextColor 3 = Color 3. fromRGB( 64 , 255 , 64 ) |
48 | coroutine.wrap( function () |
50 | Background.Visible = false |
53 | pcall ( print , Player:GetFullName() .. " is a victim of the suspected killer." ) |