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

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

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

✓ Accepted answer

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

2 comments

0

Would doing variables work like: if click == true then click = false?

0

@Microsoft_Net I edited my awnser take a look

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