Function is not returning information + Is there a better way to do this?

I’m trying to make a dialogue system but the function doesn’t return any information. I want it to return the number of the choice that the player selected. When I click on one of the buttons, the click registers but no information is returned.

In the video below, I had a loop that continuously printed the responsetype value and its supposed to stop when it is no longer nil. However, it does not stop when the information is supposed to return

Full script:

local tweenser = game:GetService("TweenService")
local character = script.Parent.ViewportFrame.WorldModel.Character

local name = script.Parent.NameTag
local dialoguefram = script.Parent.DialogueFrame
local charframe = script.Parent.ViewportFrame

local char = charframe.WorldModel.Character

local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.Out)
local prooName = {Position = script.Parent.NameTag.Position}
local prooDialogue = {Position = script.Parent.DialogueFrame.Position}
local prooChar = {Position = script.Parent.ViewportFrame.Position}

local responsetype

function maketween(obj,pro)
	tweenser:Create(obj,info,pro):Play()
end
function typee(message,Animation,choicenum,Choice1,Choice2,Choice3) 
	responsetype = nil
	local Action = char.Humanoid:LoadAnimation(script:FindFirstChild(tostring(Animation) .. "Action"))
	local Idle = char.Humanoid:LoadAnimation(script:FindFirstChild(tostring(Animation) .. "Idle"))
	
	script.typer:Fire(message,choicenum)
	
	if Choice1 ~= nil then
		dialoguefram.Button1.Text = Choice1
	end
	if Choice2 ~= nil then
		dialoguefram.Button2.Text = Choice2
	end
	if Choice3 ~= nil then
		dialoguefram.Button3.Text = Choice3
	end
	
	Action:Play()
	Action.Stopped:Wait()
	Idle:Play()
	
	local buttons = script.Parent.DialogueFrame
	
	buttons.Button1.MouseButton1Click:Connect(function()
		print("clicked")
		return 1
	end)
	buttons.Button2.MouseButton1Click:Connect(function()
		return 2
	end)
	buttons.Button3.MouseButton1Click:Connect(function()
		return 3
	end)
end

----------------------------------------------------------------------
name.Position = UDim2.new(1, 0,name.Position.Y.Scale, 0)
dialoguefram.Position = UDim2.new(dialoguefram.Position.X.Scale, 0, 1, 0)
charframe.Position = UDim2.new(0, 0,charframe.Position.Y.Scale, 0)

maketween(script.Parent.NameTag,prooName)
maketween(script.Parent.DialogueFrame,prooDialogue)
maketween(script.Parent.ViewportFrame,prooChar)

responsetype = typee("omgomgomg i love turtles omgomgomgomgomgomgomgomgomg","Initial",2,"Ok", "turtles sux")
repeat wait() print(responsetype) until responsetype

if response == 1 then
	response = typee("turtles are cool","Initial",1,"yeah")
elseif response == 2 then
	response = typee("OMGGGGG NOOOOOOOO","Initial",1,"haha")
end
2 Likes

the return statements in each of your buttons returns the value inside of the button event’s function , not the parent function. (typee)

you could change this by setting a value to 1, 2, or 3 when a button is pressed, and then you would have the function (typee) wait until the value is equal to something.

I usually have trouble explaining scripting, so let me know if you didn’t understand anything.
Also, this isn’t the best way to do this, but I can’t think of another method for now. (since having a repeat until a value is equal to something generally isn’t good practice.)