Im making a murder mystery type game and i have guis to display the role of the player when the round starts. The game is locked in first person so i cant add a button and i want it to automatically close after 4 seconds. however, everything i try wont work. here is my code:
local killerWeapon = game.ServerStorage.Knife local plrs = game.Players local inRound = game.ReplicatedStorage.inRound local survivors = {} inRound.Changed:Connect(function() if inRound.Value == true then local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())] chosen.PlayerGui.Picker.Background.RoleGiven.Text = "Murderer" chosen.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(255, 0, 0) chosen.PlayerGui.Picker.Background.Visible = true killerWeapon:Clone().Parent = chosen.Backpack print("killer") for i, plr in pairs(plrs:GetChildren()) do if plr ~= chosen then table.insert(survivors, plr) plr.PlayerGui.Picker.Background.RoleGiven.Text = "Survivor" plr.PlayerGui.Picker.Background.RoleGiven.TextColor3 = Color3.fromRGB(0, 255, 0) plr.PlayerGui.Picker.Background.Visible = true print("survivor visible") end end end end)
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.
local PlayersService = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local ServerStorage = game:GetService("ServerStorage") local BoolValue = ReplicatedStorage:WaitForChild("inRound") local Knife = ServerStorage:WaitForChild("Knife") local Survivors = {} ReplicatedStorage:WaitForChild("inRound").Changed:Connect(function(property) if tostring(property) == "Value" then local inRound = BoolValue.Value if inRound == true then local Chosen = PlayersService:GetPlayers()[math.random(1, #PlayersService:GetPlayers())] local PlayerGui = Chosen:FindFirstChildWhichIsA("PlayerGui") or Chosen:WaitForChild("PlayerGui") local Picker = PlayerGui:WaitForChild("Picker") local Background = Picker:WaitForChild("Background") local RoleGiven = Background:WaitForChild("RoleGiven") RoleGiven.Text = "Murderer" RoleGiven.TextColor3 = Color3.fromRGB(255, 64, 64) coroutine.wrap(function() wait(4) Background.Visible = false local BackpackItem = Knife:Clone() BackpackItem.Name = "Knife" BackpackItem.Parent = Chosen:FindFirstChildWhichIsA("Backpack") or Chosen:WaitForChild("Backpack") end)() pcall(print, Chosen:GetFullName() .. " is now the suspected killer.") for _, Player in pairs(PlayersService:GetPlayers()) do if Player ~= Chosen and Player.Character and typeof(Player.Character) == "Instance" and Player.Character:IsDescendantOf(workspace) then local Humanoid = Player.Character:FindFirstChildWhichIsA("Humanoid") if Humanoid:GetState() ~= Enum.HumanoidStateType.Dead and Humanoid.Health > 0 then table.insert(Survivors, Player) local PlayerGui = Player:FindFirstChildWhichIsA("PlayerGui") or Player:WaitForChild("PlayerGui") local Picker = PlayerGui:WaitForChild("Picker") local Background = Picker:WaitForChild("Background") local RoleGiven = Background:WaitForChild("RoleGiven") RoleGiven.Text = "Victim" RoleGiven.TextColor3 = Color3.fromRGB(64, 255, 64) coroutine.wrap(function() wait(4) Background.Visible = false end)() pcall(print, Player:GetFullName() .. " is a victim of the suspected killer.") end end end end end end)
Just add a wait()
...??
-- between lines 37 and 38 maybe wait(4) for i, plr in pairs(plrs:GetChildren()) do plr.PlayerGui.Picker.Background.Visible = false end