Making Events on ModuleScript

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make Events on module script similar to this:
    Making events on objects - #7 by sleitnick
  2. What is the issue? Include screenshots / videos if possible!
    ‘Attempt To Index Function With Connect’
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    None Dont know how to fix this., And Cant find anything regarding to this…
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

ModuleScript:

function AziriCast.Collided() -- checks if ray collided then fires the event.
	if AziriCast == false then return end

	local CollidedEvent = Instance.new("BindableEvent")

	local self = setmetatable({
		Collided = CollidedEvent.Event;
	},AziriCast)

	if ray.Instance then
		CollidedEvent:Fire()
	end
	
	return self
end

Script:

AziriCast.Collided:Connect(function(result)
	print(result)
end)
1 Like

Sadly, you can’t put events in module scripts. It really should be a feature though.

How do you think i could make a similar result with the one i posted? ive seen some scripts done this before but i basically cant read anything of it

1 Like

You’ll get the mouse to be returned, so you can just put the event in the listening script.

What do you mean by events, do you mean a callback function?

function AziriCast.new()
	local self = setmetatable({
		_Connections = {}
	}, AziriCast)

	return self
end

function AziriCast:OnCollided(callback)
	assert(typeof(callback) == "function", "Invalid Argument!")
	local index = table.insert(self._Connections, callback)
	
	return {
		Disconnect = function()
			table.remove(self._Connections, index)
		end,
	}
end

function AziriCast:Fire(...)
	for _, Connection in ipairs(self._Connections) do
		coroutine.wrap(Connection)(...)
	end
end

-- Separate Script
local Test = AziriCast.new()

local Connection = Test:OnCollided(function(Result)
	print(Result)
end)

Test:Fire("Foo")
Connection:Disconnect()

Could you maybe explain what this script does? i dont quite understand it thanks!

  • The OnCollided method is the connection similar to bindable events Event:Connect(...).
  • Fire method well just call the function on self._Connections table.

I’m not sure if this is the events that you where looking for, without using bindable events.

1 Like

Okay, ill try it out i think it is the event im looking for.

Im not really sure on how to import this since i dont really understand i dont think im in the level needed for it, do you have any documentation links that matches this? like assert, self, _connections

  • assert is just an if statement, if the condition returns false it will error, the sconed argument is the message. More info about assert

  • self is the reference of the variable which in this case local Test = AziriCast.new(). More information about OOP

  • _Connections is the table which stores the functions that called the OnCollided method.

1 Like

Thank you! will experiment on it and check if it works, using a similar method!

well did you try to require “ArziCast” module and call Colided() function?

I mean:

local ArziCast = require(path).Collided()

AziriCast.Collided:Connect(function(result)
	print(result)
end)
local ArziCast = require(path)

AziriCast.Collided:Connect(function(result)
	print(result)
end)

i did it like this,

edit the first line:

local ArziCast = require(path).Collided()

use a signal module like GoodSignal and just create an event that you fire whenever just as it were a BindableEvent

im trying to make my own event without relaying on other modules.

Dosent work, and i didnt expect it to work either, im going to try MakerDoe’s Solution

im trying to test this on another script, how could i? i still really dont understand how it really works :frowning:

why? just use a module that works well