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

Is there ANY way to attach an if statement to a click detection?

What I have below is a little snippet of code for a GUI dialogue system. It's using a typewriter effect. The problem is if the player spams the button to skip the text, text starts overlapping and it gets really weird, this is a bit of it:

for i = 1, #text do
		gui.Dialogue.DialogueBox.Text.Text = string.sub(text, 1, i)
		wait()
		end

What I'm trying to do is have the script ask for each iteration of the for loop if the player is pressing "MouseButton1Down" or just clicking at all, so that I can break the for loop and go on to the next section of code which is supposed to be another one of these loops, just with different dialogue of course.

1 answer

Not tested, but hope it work:

for i = 1, #text do
	gui.Button.MouseButton1Down:Connect(function()
		break
	end)
	gui.Dialogue.DialogueBox.Text.Text = string.sub(text, 1, i)
	wait()
end

Hope it helped you :D

EDIT: I realize that break is only work when it put in a loop, so that method I provide above won't work. Here is another way that I tested and worked:

local breakLoop = false -- we need this var to check is the player clicked
for i = 1, #text do
	gui.Button.MouseButton1Down:Connect(function()
		breakLoop = true -- if the gui clicked, then set breakLoop to true
	end)
	if breakLoop then -- if breakLoop == true then break
		break
	end
	gui.Dialogue.DialogueBox.Text.Text = string.sub(text, 1, i)
	wait()
end

3 comments

0

That's not how events work.

0

Oh so... I have to edit the code...

1

I don't think you understand what you're doing. Please at least test your method before posting an answer. If you're not knowledgeable on the issue's concept it's fine to not attempt answering and instead learn it beforehand.

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