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

How do i Turn off when i press the GUI again?

Im trying to do a button GUI that teleports you to mouse's CFrame, however i don't know how to turn off when press the GUI again. Here's the script:

local botao = script.Parent
local player = game.Players.LocalPlayer
local nome = player.Name
ligado = false

botao.MouseButton1Down:connect(function()
     if ligado == false then
		local mouse = player:GetMouse()
	mouse.Button1Down:connect(function()
		local loca = mouse.Hit.p
		workspace[nome].HumanoidRootPart.CFrame = CFrame.new(loca)
		ligado = true
		end)
        elseif ligado == true then
	local mouse = player:GetMouse()
	ligado = false
	
end
end)

How to do this turn off when i press the GUI again?

2 comments

0

What do you mean like turn off the script?

0

turn off the mouse teleports

1 answer

✓ Accepted answer

The best way to do this, in my opinion, is saving the event as a RBXScriptConnection in a variable, then disconnecting it.

Example:

local botao = script.Parent.TextButton
local player = game.Players.LocalPlayer
local nome = player.Name
ligado = false
-- Connection variable, to store the RBXScriptConnection in.
local connection

botao.MouseButton1Down:connect(function()
	if ligado == false then
        	local mouse = player:GetMouse()
		-- When we hook onto the event, set the connection variable to the function we're calling
     		connection = mouse.Button1Down:connect(function()
        		local loca = mouse.Hit.p
        		workspace[nome].HumanoidRootPart.CFrame = CFrame.new(loca)
        		ligado = true
        	end)
        elseif ligado == true then
    		local mouse = player:GetMouse()
    		ligado = false
		-- Disconnect from the connection
		connection:Disconnect()
end
end)


You can read more about RBXScriptConnection here!

1 comment

0

Nice, it worked, thanks!

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