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

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

Asked by
caipira 29
7 years ago

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?

0
What do you mean like turn off the script? jinsouGod 0 — 7y
0
turn off the mouse teleports caipira 29 — 7y

1 answer

Log in to vote
0
Answered by
DevNetx 250 Moderation Voter
7 years ago

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!

0
Nice, it worked, thanks! caipira 29 — 7y
Ad

Answer this question