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

TextButton only working once, doesn't work after being clicked once?

Asked by 3 years ago

So, I'm making a spectate button, and I'm trying to make it so when you press the "exit" button, the spectate gui will close. However, this works, but only once for some reason. Help would be appreciated,

(These are both local scripts)

script 1 (inside the button that opens the gui)

local player = game.Players.LocalPlayer
local open = false
script.Parent.MouseButton1Click:Connect(function()
    if open == false then
        open = true
        local Spectating  = Instance.new("BoolValue")
        Spectating.Name = "Spectating"
        Spectating.Parent = player
        script.Parent.Parent.Background.Visible = true
        print("Visible")
        end
end)

script 2 (Inside the gui)

script.Parent.Exit.MouseButton1Click:Connect(function()
    if plr:FindFirstChild("Spectating") then
        plr.Spectating:Destroy()
        script.Parent.Visible = false
        print("NotVisible")
    end
end)

1 answer

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

In script 1, "open" is never set back to false, therefore, the if statement on line 4 will only ever be true once.

Instead of using a variable (which will never get set back to false), check if the background frame is visible:

local player = game.Players.LocalPlayer
local backgroundFrame = script.Parent.Parent.Background
script.Parent.MouseButton1Click:Connect(function()
    if backgroundFrame.Visible == false then
        local Spectating  = Instance.new("BoolValue")
        Spectating.Name = "Spectating"
        Spectating.Parent = player
        backgroundFrame.Visible = true
        print("Visible")
        end
end)
0
Thank you very much, it works! DriBowser 55 — 3y
Ad

Answer this question