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?
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!