[SOLVED] Problems with MouseButton1Click

A quick update to the situation.

I added this code to print the parent of NextButton.

while wait(1) do
	print(NextButton.Parent)
end

The correct parent is Question Template. It printed Question Template until I hit the “Ready” button.

The parent is not nil, because I went into PlayerGui and took this screenshot.

So there is something stopping my code.

Do you guys have any ideas on what is stopping my code?

local PlayerGui = game.Players.LocalPlayer.PlayerGui
local TransparentLayer = PlayerGui:WaitForChild("Application")["Base"]["Transparent Layer"]
local IntroductionTemplate = TransparentLayer["Introduction Template"]
local ReadyButton = IntroductionTemplate:FindFirstChild("ReadyButton")
local NextButton = TransparentLayer["Question Template"]["NextButton"]

IntroductionTemplate.Visible = true

local SavedAnswers = {}

local TimeLimit = 300
local NotedTimeLimit = 300

local function BaseFunction(QuestionNum, Question)
	local QuestionString = "Q"..QuestionNum..". "..Question
	local QuestionTemplate = TransparentLayer["Question Template"]

	if IntroductionTemplate then
		local HidingPos = UDim2.new(2,0,0,0)
		local TargetPos = UDim2.new(0.012,0,0,0)
		IntroductionTemplate:TweenPosition(HidingPos, "Out", "Quint", 1)
		repeat wait() until IntroductionTemplate.Position == HidingPos
		IntroductionTemplate:Destroy()
		
		local QuestionBox = QuestionTemplate.QuestionBox
		QuestionBox.Text = QuestionString
		
		QuestionTemplate.Visible = true
		QuestionTemplate:TweenPosition(TargetPos, "Out", "Quint", 1)
		return
	end
	
	if TransparentLayer:FindFirstChild("Question Template") then
		local Question = TransparentLayer:FindFirstChild("Question Template")
		local HidingPos = UDim2.new(2,0,0,0)
		local TargetPos = UDim2.new(0.012,0,0,0)
		Question:TweenPosition(HidingPos, "Out", "Quint", 1)
		repeat wait() until Question.Position == HidingPos
		
		local QuestionBox = Question.QuestionBox
		QuestionBox.Text = QuestionString
		
		Question.Visible = true
		Question:TweenPosition(TargetPos, "Out", "Quint", 1)
	end
end

function sendTrelloResults()
	
end

if TransparentLayer:FindFirstChild("Introduction Template") then
	local IntroductionTemplate = TransparentLayer:FindFirstChild("Introduction Template")
	local ReadyButton = IntroductionTemplate:FindFirstChild("ReadyButton")
	ReadyButton.MouseButton1Click:Connect(function()
		BaseFunction("1", "If you were to greet a customer at the cafe, what would be your greeting?")
	end)
end

while wait(1) do
	print(NextButton.Parent)
end

NextButton.MouseButton1Click:Connect(function()
	print("got to the clicked function")
	local Times = 0
	if Times == 0 then
		Times = Times + 1
		BaseFunction("2", "Why do you choose to work at Reiné over other companies?")
	elseif Times == 1 then
		Times = Times + 1
		BaseFunction("3", "Why do you feel like you should be accepted over other applicants & what special abilities do you possess?")
	elseif Times == 2 then
		Times = Times + 1
		BaseFunction("4", 'Correct this sentence: Hay welcome to rénay cafwe. Im gladd too be takinq youre ordier todeay howw may i helpp!')
	end
end)

Sorry can u check if this one is a infinite loop your script seems to be yielding for another function

It’s not an infinite loop. In addition, I put prints all over my code and things are all working properly.

You should try defining this variable outside of the OnNBClick function. Otherwise it is always going to be changed to 1.

2 Likes

I think, just to make the folks happy, since the repeat wait() is causing some controversy (you shouldn’t use it, by the way), I’ll provide you with a better option.

    if IntroductionTemplate then
		local HidingPos = UDim2.new(2,0,0,0)
		local TargetPos = UDim2.new(0.012,0,0,0)
		IntroductionTemplate:TweenPosition(HidingPos, "Out", "Quint", 1, false, function()
            IntroductionTemplate:Destroy()
            local QuestionBox = QuestionTemplate.QuestionBox
	        QuestionBox.Text = QuestionString
		
		    QuestionTemplate.Visible = true
	    	QuestionTemplate:TweenPosition(TargetPos, "Out", "Quint", 1)
        end)
		
		
		return
	end
1 Like

Thanks for the tip, but still nothing has changed. :thinking:

1 Like

It is not meant to change it, but you shouldn’t use repeat wait at all. As an FYI, what I did was use the function parameter in TweenPosition that gets called after the tween is done. It’s much safer than calling wait() until you check the positions directly. I didn’t expect it to fix the problem, but using wait() could cause problems in the future.

1 Like

Could u try this?
Changing the repeats with Waits

local function BaseFunction(QuestionNum, Question)
	local QuestionString = "Q"..QuestionNum..". "..Question
	local QuestionTemplate = TransparentLayer["Question Template"]

	if IntroductionTemplate then
		local HidingPos = UDim2.new(2,0,0,0)
		local TargetPos = UDim2.new(0.012,0,0,0)
		IntroductionTemplate:TweenPosition(HidingPos, "Out", "Quint", 1)
		wait(1)
		IntroductionTemplate:Destroy()
		
		local QuestionBox = QuestionTemplate.QuestionBox
		QuestionBox.Text = QuestionString
		
		QuestionTemplate.Visible = true
		QuestionTemplate:TweenPosition(TargetPos, "Out", "Quint", 1)
		return
	end
	
	if TransparentLayer:FindFirstChild("Question Template") then
		local Question = TransparentLayer:FindFirstChild("Question Template")
		local HidingPos = UDim2.new(2,0,0,0)
		local TargetPos = UDim2.new(0.012,0,0,0)
		Question:TweenPosition(HidingPos, "Out", "Quint", 1)
		wait(1)
		
		local QuestionBox = Question.QuestionBox
		QuestionBox.Text = QuestionString
		
		Question.Visible = true
		Question:TweenPosition(TargetPos, "Out", "Quint", 1)
	end
end

If this doesnt work then I’m out of ideas.

I’m going to stick with @tralalah’s idea. Plus, I don’t think that would make a difference personally. Thanks for the suggestion, though!

@Aerosphia could you possibly right click the ScreenGui > Save to file and upload it here so that people can debug it in their studios without taking rough guesses?

1 Like

Here is the ScreenGui and all of the code:
DebugThis.rbxm (11.1 KB)

I get it. I know why now. You have your script in Introduction Template, but you destroy it so the script doesn’t exist. Try moving it to The Question Template. That should fix it.

1 Like

image
Yep that was the issue all along

1 Like

LOL! Thanks. I wasn’t expecting that as the problem. Silly me. :laughing:

1 Like