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

How would you make a variable so when a button is clicked again it does a function?

Asked by 4 years ago

I am still learning to become a better roblox UI builder, and I have tried doing this before but normally it wouldn’t function and I have to use while true do to make it work, so I am wondering how you would do this in a better way?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

If i get this right, i would do it in the following way:

Local Script in a TextButton (does not need to be in text button.)

local button = script.Parent -- TextButton
local timesclicked = 0 -- the var name says much.

function click() -- creating and naming a function
    if timesclicked == 0 then
        timesclicked = timesclicked + 1 -- add 1
    elseif timesclicked == 1 then
        print("Hello Worldy!") -- script here
    timesclicked = 0 --reset
    end
end
button.MouseButton1Down:Connect(click)

Representation: Here

And you can do the same for parts!

Server Script on a part:

local Detector = script.Parent:WaitForChild("ClickDetector")
local timesclicked = 0 -- the var name says much.

function click() -- creating and naming a function
    if timesclicked == 0 then
        timesclicked = timesclicked + 1 -- add 1
    elseif timesclicked == 1 then
        print("Hello Worldy!") -- script here
    timesclicked = 0 --reset
    end
end
script.Parent.ClickDetector.MouseClick:Connect(click)

Representation: Here

Remember that whatever happens in a local script is only client side, you will need Remotes to do anything with a part in workspace for example, anything supposed for other players to see unless you are modifying player's parts (not adding).

Edit: For switching variable value:

Local Script in a TextButton (does not need to be in text button.)

local button = script.Parent -- TextButton
local Variable = false

function click() -- creating and naming a function
    if Variable == false then
    variable = true
        else
    varialbe = false
    end
end
button.MouseButton1Down:Connect(click)

Representation: Here

0
Would doing variables work like: if click == true then click = false? Microsoft_Net 21 — 4y
0
@Microsoft_Net I edited my awnser take a look Igoralexeymarengobr 365 — 4y
Ad

Answer this question