Can't pass arguments to function

I’m trying to pass arguments to a function but I’ve come across a logic puzzle where I don’t know how to pass arguments.

I’m creating a script for dialogue which iterates through player responses to an NPCs message. If the player presses the designated function connected to the button, they will go so on and so on.

The problem is if I were to click options(buttons/responses to NPCs) a bajillion times, I would make the scripts go haywire so I need a debounce.

--
local UI = script.Parent.UI
local BankGui = UI.BankGui
local Holders = UI.Holders
--
local DialogueGui = UI.DialogueGui
local Dialogue = DialogueGui.Dialogue
local DialogueText = Dialogue.Frame.DialogueText
local TitleFrame = DialogueGui.TitleFrame
local Title = DialogueGui.TitleFrame.TextLabel
local Options = DialogueGui.Options
--
local function typewrite(message,text,length)
	for i = 1,#message,1 do
		text.Text = string.sub(message,1,i)
		task.wait(length)
	end
	task.wait(1)
end

local function destroyoptions()
	for i,v in Options:GetChildren() do if v:IsA("TextButton") then v:Destroy() end end
end

--Modules
local Bank = require(script.Bank)

local Dialogue = {}


Dialogue.Messages = {
	["Gyago"] = {
		Init = "Hey, what do you need?",
		[1] = {
			["Nothing"] = function(option)
				local enabled = option:GetAttribute("Enabled")
				if not enabled then return end
				option:SetAttribute("Enabled",false)
				typewrite("...",DialogueText,0)
				DialogueGui.Enabled = false
				destroyoptions()
			end,
			["I want to sell my items"] = function(option)
				local enabled = option:GetAttribute("Enabled")
				if not enabled then return end
				option:SetAttribute("Enabled",false)
				typewrite("Ok!",DialogueText,0)
				DialogueGui.Enabled = false
				Bank:Init()
				destroyoptions()
			end,
		}
	},
	["Oogli"] = {
		Init = "Hello, anything you need upgraded today?",
		[1] = {
			["Yeah, i'd like my items upgraded"] = function()
				
			end,
			["Nah"] = function()
				
			end,
		}
	}
}

function Dialogue:Init(npc)
	local npcMessage = Dialogue.Messages[npc]
	local init = npcMessage.Init
	local responses = npcMessage[1]
	Title.Text = npc
	DialogueGui.Enabled = true
	typewrite(init,DialogueText,0)
	for i,v in responses do
		self:CreateOption(i,v)
	end
end

function Dialogue:CreateOption(message,func)
	local option = Holders.Option:Clone()
	option.Text = message
	option.Parent = Options
	option.MouseButton1Up:Connect(func,option)
end

return Dialogue

The main source of my problem is in Dialogue:CreateOption and Dialogue.Messages. I am trying to pass the parameter option into a connection but the function won’t recognize it as an argument.

I don’t see how I could make this a high order function as I am not directly accessing the function and it does not have a name.

Any ideas?

2 Likes

Have you tried using variables? If not, maybe try them?

1 Like

option.MouseButton1Up:Connect(function()
func(option)
end)

local db=true --defined way up top

option.MouseButton1Up:Connect(function()
   if db then db=false
     responses[message](option) -- figured you would know this.
     task.wait(2) -- whatever works 
     db=true
   end
end)

yes but func is not the actual name of the function its just a placeholder name for the argument which is the function.

func is not the function’s name.

If I used variables then it wouldn’t work how I intend it to because I wouldn’t know what functions are associated with the player responses.

option.MouseButton1Up:Connect(function()
	responses[message](option)
end)