Calling a method with :Connect()

So I am trying to find out if it is possible to call methods using :Connect()
I know it is possible by using :Connect(function()), however i wanna call the method right away

Example of what i wanna achieve:

game.ReplicatedStorage.Event.OnServerEvent:Connect(module.method)

If anything is unclear please ask more questions so I can explain it better.

1 Like

If I’m not mistaken, you should be able to do that. Although if you want to use parameters, you’ll want to use an anonymous function.

Yes, but you know how methods take self as the first argument? Well I’m trying to put that in through connect, to keep the code more readable and not have to readjust things everytime i change something in a fire event call.

Well, it looks like it should. I did the following:

local function OnPlayerAdd(self)
	print(self)
end

game:GetService("Players").PlayerAdded:Connect(OnPlayerAdd)

And got:

tinctoriall

(that’s my username, so yes, it works).

1 Like

Sorry for being so late, thats not what im looking for tho.
i will show an example of what i wanna do

lets say this is a module that has a method to type out a given value

local module = {}

function module:type(message: string)
     print(message)
end

return module

and this is the script that calls it:

Event.OnServerEvent:Connect(module.type)

(assume the values are all sent from the client)

so this would not work because of the first argument being self
so what i tried is doing :Connect(module:type)
that did not work

Right.
Well I just tried doing something similar and it worked, but I don’t know if it’d help you.

Module:

local module = {}

function module:type()
	print(self)
end

return module

Script:

local module = require(script.Parent.ModuleScript)
game.Players.PlayerAdded:Connect(module.type)

It still prints “tinctoriall”. Although I know this is all managed from the server and not from the client as you want.

ok so this is almost what ive been looking for but ill put an example of my code and the issue im having

function Tower:Spawn(player: Player, position: CFrame, towerName: string)
	if TowerFolder:FindFirstChild(towerName) and player.PlayerStats.Cash.Value >= require(towerStats:WaitForChild(towerName)).Level1.UpgradePrice then
		player.PlayerStats.Cash.Value -= require(towerStats:WaitForChild(towerName)).Level1.UpgradePrice
		player.PlayerStats.TowersPlaced.Value += 1

so the issue here is that when it is called, all the values get messed up and theres errors because of it
example: the player is turned to some numbers, position to the player name and towername to nil

Is this function being called via server-script through a Remote Event? Because since you have more than 1 parameter, I’m pretty sure you’ll want to make an anonymous function when using the connection to pass them through (which is your issue). I searched up on it, and this post says the same.

its being called on server side only (it is parented to a script in serverscriptservice) and happens when a remoteevent is fired

also remember if it is not very doable i can just use anonymous functions without much problem (just really curious if it can be done)

Well, by looking at what you need, you’ll want to, yes, use an anonymous function. Sorry about that

1 Like

this code should work. no anonymous function needed. dont use the : unless you called self, which is for metatables.

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