Can you not use functions in functions in oop?

i know the title seems weird but

local Chat = {}
Chat.__index = Chat

local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local TI = TweenInfo.new(0.5, Enum.EasingStyle.Circular, Enum.EasingDirection.Out)
local StarterGui = game:GetService("StarterGui")

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

function Chat.new()
	local self = setmetatable({}, Chat)
	self.UI = script.ChatUI:Clone()
	self.UI.Parent = Player.PlayerGui
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
	return self
end

function Chat:BindToSlash()
	ContextActionService:BindAction("Focus", self:Focus(), false, Enum.KeyCode.Slash)
end

function Chat:Focus(actionName, inputState, inputObject)
	if actionName == "Focus" and inputState == Enum.UserInputState.End then
		self.UI.Frame.InputText:CaptureFocus()
	end
end

function Chat:SendMessage()
end

return Chat

at function Chat:BindToSlash, self:Focus() says its nil. Why??

Are you getting an error in the output window? If you’re only seeing a warning in the script editor this is likely just due to the current state of the Luau type solver. It does not play well with the typical implementation of Lua metatable OOP at the moment.

yeah Argument 2 Missing or nil

Oh, well self:Focus() returns a nil value (the method implementation has no return so nil is the default return. I think what you might be going for is:

function Chat:BindToSlash()
	ContextActionService:BindAction("Focus", function() self:Focus() end, false, Enum.KeyCode.Slash)
end
2 Likes

oh that worked :sob:

why do i need to wrap it in a function though… isnt self:Focus() a function

No, consider this line alone:

local x = self:Focus()

This line calls the method Focus, therefore x is set to the return value from the method call (nil in this case). Instead, if you do:

local y = function() self:Focus() end

The variable y is now a function value. In order to actually run the function you would need to do y()

but in a localscript i can jsut go

local ChatUI = require(ReplicatedStorage.Chat).new()
ChatUI:Focus()

why wouldnt i have to do

local y = function() ChatUI:Focus() end

In that example you are calling the method immediately in that line. This is perfectly valid if you want the contents of the Focus method to run when that line runs.

However, from you original issue, you need to pass a function value (ChatUI:Focus() is not a function value, it is the value returned after calling that method) as the second argument to BindAction. You don’t want to call Focus immediately when calling BindAction so instead we need to pass BindAction a function it can call (after some key is pressed). Therefore, we need to wrap the method call into an anonymous function (or one defined earlier).

1 Like

ohhhh okay, thanks for explaining i really appreciate it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.