Problems with Preventing Unwanted Behaviours

Problems with Preventing Unwanted Behaviours

This is a very simple script, that will basically act as a submission centre where I will eventually make a HTTP Request to perhaps post something later. However, I am having issues with the User Interface aspect (no errors, just it’s not working as intended). I would appreciate it if any of you could provide other work-arounds to ensure the expected behaviour is yielded at the end. This is the script below:

local debounce = false
local main = script.Parent.Parent
local q1 = main.One
local err = main.Error
local q2 = main.Two
local nxt = main.Next
local back = main.Back
function clicked()
	if q1.Visible then -- Question 1: Complaintant(s)
		if #q1.Content.Text > 0 then
			if err.Visible then
				err.Visible = false
			end
			q2.Visible = true
			q1.Visible = false
			back.Visible = true
			script.Parent.Position = UDim2.new(0.525, 0,0.814, 0)
		else -- error handling
			if debounce then return end
			err.Text = script.Parent.Parent.Error.Text .. " You need to input a complaintant(s)."
			err.Visible = true
			debounce = true
			for i = 0, 3,1 do wait(1) end
			err.Visible = false
			debounce = false
		end
	end
	if q2.Visible then
			if q2.Button1.Font == Enum.Font.SourceSansBold or q2.Button2.Font == Enum.Font.SourceSansBold or q2.Button3.Font == Enum.Font.SourceSansBold then
				if q2.Button1.Font == Enum.Font.SourceSansBold then
					q2.Expungement.Visible = true
					q2.Visible = false
				if err.Visible then
					err.Visible = false
				end
			end
			if q2.Button2.Font == Enum.Font.SourceSansBold then 
				if err.Visible then
					err.Visible = false
				end
				
			end
		else
			if debounce then return end
			err.Text = err.Text .. " You need to select a Type of Case."
			err.Visible = true
			debounce = true
			for i = 0, 3,1 do wait(1) end
			err.Visible = false
			debounce = false
		end
	end
end
script.Parent.MouseButton1Click:Connect(clicked)

Extra Information

Current Outcomes:

  • What I’m doing is checking if the Next button is clicked with a function called clicked.
  • Check if a GuiObject is Visible and continue onward from there.
  • For the second if statement:
if q2.Visible then

This is where my issues happen because, what I don’t want to happen is for the client to be shown an error before they have even clicked on anything. To show what I mean by this, here is a video that will pretty much sum up everything:


As you should be able to see, before anything happens, an error is prompted. However, what I want to happen is to somehow end the function after an if statement. An example (the comment):

function clicked()
	if q1.Visible then -- Question 1: Complaintant(s)
		if #q1.Content.Text > 0 then
			if err.Visible then
				err.Visible = false
			end
			q2.Visible = true
			q1.Visible = false
			back.Visible = true
			script.Parent.Position = UDim2.new(0.525, 0,0.814, 0)
		else -- error handling
			if debounce then return end
			err.Text = script.Parent.Parent.Error.Text .. " You need to input a complaintant(s)."
			err.Visible = true
			debounce = true
			for i = 0, 3,1 do wait(1) end
			err.Visible = false
			debounce = false
            -- end the function here until called again

		end
	end

To clear up, all I really want is to cut off the function at the else statement, if there is a way of doing that. But I would be appreciative of other solutions also. :slight_smile:

You should use return where you want the function to end. This ends the function and returns a value if you want as well. I’d recommend looking here for more info: Can someone Explain returning to me - #3 by blokav

1 Like

Actually that’s brilliant thank you. Didn’t know if I needed to use break or return or if another solution would work, but that’s great!

1 Like

Glad that helped! Break is used in a loop. For example if you’re using a for loop and you want to stop it when it finds something, you can use break to stop the loop and continue the rest of the script. Return is used in functions for halting the function and/or returning a value.

1 Like