How do i put this inside of a table?

table.insert(self.connections,
		self.CoreFunction.OnClientInvoke = function(Type, Action)
			local variable
			
			if Type == "M1" then
				if Action == "Hitbox" then
					variable = self:_M1Hitbox()
				end
			end
			
			return variable
		end
	)
	

self.connections = table

im doing this cause im kinda forking some of the roblox weapon controllers.
and i can clean up and maid my own scripts connections without having to do crazy stuff.

2 Likes

I don’t quite understand what the goal is here. I’m supposing you’re not trying to insert the function itself into the table. Can you elaborate a bit more?

im trying to put the function into the table, so i can clean it up easier and stuff

Your code is trying to do two things: set self.CoreFunction.OnClientInvoke to your function and store the function in the table.

You can set self.CoreFunction.OnClientInvoke to your function and store the function in the table by doing:

-- Create the function and store it in self.CoreFunction.OnClientInvoke
self.CoreFunction.OnClientInvoke = function(Type, Action)
	local variable
			
	if Type == "M1" then
		if Action == "Hitbox" then
			variable = self:_M1Hitbox()
		end
	end
			
	return variable
end)

-- Also store the function in the self.connections array
table.insert(self.connections, self.CoreFunction.OnClientInvoke)

Something to note, self.CoreFunction.OnClientInvoke is not a connection, it’s just a regular variable, in this case of the function type.

If you want it such that you can clean up the callback function stored in self.CoreFunction.OnClientInvoke, you could pass a function that removes the call back function:

table.insert(self.cleanUpFunctions, function()
	-- Clean up by setting the callback to an empty function
	self.CoreFunction.OnClientInvoke = function() end
end)

Then to clean up everything you can do:

-- Run the functions to clean everything up
for _, cleanUpFunction in ipairs(self.cleanUpFunctions) do
	cleanUpFunction()
end
2 Likes
 OnClientInvoke is a callback member of RemoteFunction; you can only set the callback value, get is not available 

??? what does this mean

Whoops, I didn’t realize that self.CoreFunction is a RemoteFunction.

The error is because the .OnClientInvoke property is write only: code is only allowed to set it, so it gives an error when the code above tries to read the property to put it into the self.connections table.

Here is some updated code that doesn’t read from the .OnClientInvoke property of the RemoteFunction:

-- Create the function
local onClientInvokeCoreFunction = function(Type, Action)
	local variable
			
	if Type == "M1" then
		if Action == "Hitbox" then
			variable = self:_M1Hitbox()
		end
	end
			
	return variable
end
-- Store it in self.CoreFunction.OnClientInvoke
self.CoreFunction.OnClientInvoke = onClientInvokeCoreFunction

-- Also store the function in the self.connections array
table.insert(self.connections, onClientInvokeCoreFunction)

Because self.CoreFunction is a RemoteEvent, you can also clean it up like so:

local function cleanUpRemoteFunction(remoteFunctoin)
    -- Set callback to empty function
    self.CoreFunction.OnClientInvoke = function() end
end

cleanUpRemoteFunction(self.CoreFunction)
1 Like
-- Define the self table (could be part of a class or module, depending on your architecture)
self = {
    connections = {}, -- Table to store connections
    CoreFunction = Instance.new("RemoteFunction") -- Placeholder for CoreFunction (change this as needed)
}

-- Example function _M1Hitbox (replace this with your actual function)
function self:_M1Hitbox()
    -- Logic for when Type is "M1" and Action is "Hitbox"
    print("M1 Hitbox logic executed!")
    return "HitboxResult" -- Return a result after execution
end

-- Insert the OnClientInvoke event into the connections table
table.insert(self.connections, 
    self.CoreFunction.OnClientInvoke = function(Type, Action)
        local variable
        
        -- Check the Type and Action passed in
        if Type == "M1" then
            if Action == "Hitbox" then
                -- Call the M1Hitbox method and store the result in 'variable'
                variable = self:_M1Hitbox()
            end
        end
        
        -- Return the result back to the invoker
        return variable
    end
)

-- Example cleanup function: Disconnect all stored connections
function self:cleanupConnections()
    for _, connection in ipairs(self.connections) do
        if typeof(connection) == "RBXScriptConnection" then
            connection:Disconnect()
        end
    end
    -- Clear the connections table after cleanup
    self.connections = {}
end

-- Example invocation of CoreFunction
-- This would typically be triggered by the client or server depending on your design
local result = self.CoreFunction:InvokeClient("M1", "Hitbox") -- Simulates the OnClientInvoke being triggered
print("Result from CoreFunction:", result)

-- Cleanup all connections when done
self:cleanupConnections()