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

How to automatically close gui after 4 seconds?!

Asked by 3 years ago

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)

2 answers

Log in to vote
1
Answered by 3 years ago

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)
0
I tried this code but the gui didnt appear. I also put some prints in but they didnt work either. YourAverageMyth 45 — 3y
0
because you suck at scripting ez ez ez ez pingsock 111 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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
0
adding a wait causes 1 player to see the survivor role, then when it dissapears the next person sees the survivor role. YourAverageMyth 45 — 3y
0
Sorry for the late reply, but i thought if you add it outside the loops like i said maybe between lines 37 and 38, it would again go through every player and instead of adding the role and making it visible, it just makes the role not visible? Omq_ItzJasmin 666 — 3y

Answer this question