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

GUI Button Click Working Improperly?

Hi, When making a single button that alternates between two options when you click it, a problem arises. The button is supposed to teleport you to one brick, but, when the second-long debounce ends, it teleports you to the other brick, as though you clicked on it again. I'm not sure what's causing this. Any ideas?

player = game.Players.LocalPlayer
button = script.Parent
local debounce = false
image = script.Parent.Parent.ImageButton.Image

function helpme()
 if not debounce then
  	if image == "rbxassetid://2730594257" then
 	debounce = true
 	image = "rbxassetid://2730605530"
 	local LowerTorso = player.Character.LowerTorso
 	LowerTorso.CFrame = game.Workspace.telepart4.CFrame
	wait(1)
	debounce = false
 	end
	if image == "rbxassetid://2730605530" then
 	debounce = true
 	image = "rbxassetid://2730594257"
 	local LowerTorso = player.Character.LowerTorso
 	LowerTorso.CFrame = game.Workspace.telepart5.CFrame
	wait(1)
	debounce = false
 	end
 end
end
button.MouseButton1Click:connect(helpme)

1 comment

1

It appears that in your function if debounce is false, both if statements will run. Hard to tell on mobile bc your indentations are off

1 answer

✓ Accepted answer

I Think it is because you forgot the else, So what I think was happening is that the game was checking the first If statement then it waited 1 second and checked the second one, and it executes both statements each click instead of executing 1 each time you click. (Also I Marked The Place I Added An Else)

Try This:

player = game.Players.LocalPlayer
button = script.Parent
local debounce = false
image = script.Parent.Parent.ImageButton.Image

function helpme()
 if not debounce then
    if image == "rbxassetid://2730594257" then
    debounce = true
    image = "rbxassetid://2730605530"
    local LowerTorso = player.Character.LowerTorso
    LowerTorso.CFrame = game.Workspace.telepart4.CFrame
    wait(1)
    debounce = false
else if image == "rbxassetid://2730605530" then ------------------------------------------- I added an else here so then you need to click a second time to check what image it is
    debounce = true
    image = "rbxassetid://2730594257"
    local LowerTorso = player.Character.LowerTorso
    LowerTorso.CFrame = game.Workspace.telepart5.CFrame
    wait(1)
    debounce = false
    end
 end
end
button.MouseButton1Click:connect(helpme)

(I Havent Tested It So Sorry If It Doesn't Work)

3 comments

0

that wont matter bc the images are clearly different. it cant be both!!

1

After adding the "not debounce" to each if, and changing it to an if else, it worked! Thank you.

0

:)

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