Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

Thank you very much, it works!

This discussion is preserved as it was. New answers, comments, and votes are closed.