Ok, so basically this I'm having a bad time trying to fix this.
I have a Module script called "ServerPlayer" (used in a non-local script in the workspace), which contains a set of functions (ServerPlayer:addPlayerRequest(), ServerPlayer.setStatusRequest()...). I have a RemoteEvent object which the player requests any function of this module. For example, a player could add itself by running "remoteEvent:fireServer('addPlayerRequest',LocalPlayer.Name)". This is my code:
ServerPlayer = require(game.ReplicatedStorage.Classes.Core.ServerPlayer) local serverPlayer = ServerPlayer.new() local ReplicatedStorage = game:GetService("ReplicatedStorage") local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") function runRequest(player, functionName,...) print("Request from player "..player.Name.." received! Function name: "..functionName..". Args:",unpack({...})) serverPlayer[functionName](...) end remoteEvent.OnServerEvent:Connect(runRequest)
However, for some reason, whenever a player sends a requests from a local script and calls any function, the self clause within the module becomes null for some reason. For example, the module has a property called "LocalData". Whenever a player requests to call the addPlayer function, It prints the following error:
ReplicatedStorage.Classes.Core.ServerPlayer:31: attempt to index nil with 'LocalData'
An exception is returned because of "self.LocalData". However, whenever I call manually this function by writting something like:
serverPlayer:addPlayerRequest("Player1")
It works fine, but when I do It like:
serverPlayer["addPlayerRequest"]("Player1")
The self clause becomes null and the code fails. Any idea of how to fix this? (Sorry for my english)
serverPlayer["addPlayerRequest"](serverPlayer,"Player1") -- is the same as serverPlayer:addPlayerRequest("Player1")
The functions you access with colon notation are methods. Essentially they’re functions that automatically pass the object the method is a member of to the method itself. This is why you’re able to use the self variable in any method. This is also why you can awkwardly call the method with dot notation (or by indexing with the function name as a string) so long as you include the object as the first parameter.
Alright, I found out you have to write it like:
serverPlayer["addPlayerRequest"](serverPlayer,"Player1")
Have no idea why, but it works fine now.