Chat enabling and disabling script not working

This is a script I have, it’s supposed to enable the chat when vis.Value is true (Which gets set to true in another script), the script works to disable the chat but does not reenable it.
Script:

local Player = game.Players.LocalPlayer
Player.PlayerGui:SetTopbarTransparency(1)
local playergui = game.Players.LocalPlayer.PlayerGui
local gui = playergui:WaitForChild("TeamSelect")
local val = gui.vis
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
if val.Value == true
then
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, true)
end

An if statment only checks once.

Meaning, if you had a part in workspace, and you had a script that checks if the part’s name was Bob.

if workspace.Part.Name == "Bob" then
    print("hi Bob")
end

Let’s say when the game first started, the part’s name wasn’t Bob, it was something else, but after 5 seconds or so it was changed to Bob. Will the if statment pass? The answer is no, because it already checked if its name was Bob when the game first ran, it won’t check again. In order to make it constanly check, you have to put it inside of a while loop, or anything else that makes it constanly check, or anything that’s supposed to alarm the if statment to check again.

In your example, the if statment is supposed to re-enable the thing when the value is set to true, we can put the if statment inside of a while loop and everything would work, but a more efficient solution would be to put it inside of a Changed event, connected to the val object, beacuse technically, we only need to check when a the .Value property of it has changed, instead of constanly doing so.

val.Changed:Connect(function()
    if val.Value == true then
        game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, true)
    end
end)
1 Like