I have a question about functions

I want to know if I can call :Connect() on my own functions

I do not know if I can do it

I have tried devforum.roblox.com and developer.roblox.com

I am trying to make my own functions in a module script but I do not know if I can call a :Connect() event on my own created function from a module script. Such as this:
Module.FunctionName:Connect(function()
But I do not know if it is possible. I would love to get some help.

--ModuleScript
local module = {}

function module.Create(instance)
	instance = Instance.new("Part",workspace)
end

return module

--ServerScript
local Mod = require(game.ReplicatedStorage.ModuleScript)

Mod.Create:Connect(function()
	--Code
end)

No, not like that. You would have to use an event.

local module = {}
local event = Instance.new('BindableEvent')
function module.Create(instance)
    instance = Instance.new('Part')
    instance.Parent = workspace -- try not to use the parent argument in Instance.new, it's a bit slower.
    event:Fire()
end
module.Created = event.Event -- use the event signal of the remote event
local mod = require(game.ReplicatedStorage.ModuleScript)
mod.Created:Connect(function()
    print('mod.Create was called!')
end)

mod.Create()
1 Like

No. :Connect() is a method that is attached to events, not functions

1 Like

I do not think Event is a valid calling of an remoteevent

It is. You can take the signal of any object and fire it and connect it to a function.

1 Like

I know I wanna try to do it really badly where I can do it I hope that I can use it with remote events

image

Oh shoot, I meant BindableEvent. Keep getting those mixed up for some reason lol.

You don’t connection functions, you’ll need to use an event instead.
Usually I just use a BindableEvent to make my own events.

local module = {}

function module.myfunc()
local bindable = Instance.new('BindableEvent')

coroutine.wrap(function()
while wait(2) do
bindable:Fire()
end
end)()

return bindable.Event
end

return module

-- script
local module = require(path.to.module)
local event = module.myfunc()

event:Connect(function()
print('Hello world!')
end)

This would print “Hello world!” every 2 seconds.

2 Likes

I like it, it works but is their a easier way without coroutine.wrap or while do loops

Then the event wouldn’t return back and would yield forever.

1 Like

Can I delete the while true do?

Yeah, you can fire the bindable event whenever you want, just make sure that you put it in a coroutine if its a loop.

I’m not a expert with modules so I might be wrong.

it’s a while wait do
also why do you want a :Connect in your module functions

It does not work now here is my coroutine:

coroutine.wrap(function()
    event:Fire()
end)

I just feel like it lol :grinning_face_with_smiling_eyes:

You don’t need to put that in a coroutine, I said you need to if its inside a loop. Also coroutine.wrap returns a function so you’ll need to call it.

Well this is not looping it only fires once

Why did this not work?

local Mod = require(game.ReplicatedStorage.ModuleScript)

Mod.Create:Connect(function()
	print("Works")
end)()

I did all the events I do not want

local event = Mod.Create()

event:Connect(function()

end)

Can you show me your current module?
(also you cant fire a connection, and i think your confused with what im saying)