Is it possible to get the Player through a module function?

Is it possible to use Player in module functions because i trying to get the player though a module but the code never prints Player like its suppose to instead it just ignores it?

Module Code:

function module:AwardUnobtainableItems(Player, ToolName)
	-- Gives Unobtainable items if the player has the badge for it.
      if Player then
        print("Player")
    end
end

Script Code:

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
        require(game.ReplicatedStorage.GameModules.GameRewards):AwardUnobtainableItems(Player)
	end)
end)

At first why are you requiring module script every time player joins, you should require it once. Secondly replace module:AwardUnobtainableItems with module.AwardUnobtainableItems and :AwardUnobtainableItems(Player) with .AwardUnobtainableItems(Player)

The code as you have it should be working unless your script is a LocalScript. Is that the exact code as you have it set up for testing? Is the script running?

1 Like

That will make self = the player, because the function is defined using :. Oh, you’re telling him to change the definition.

Yeah, exactly! :smiley:

char limit

It requires the module to award the player the item in the inventory when they first load into the game. Also, what is the difference between module.AwardUnobtainableItem(Player) and module:AwardUnobtainableItem(Player)

It’s a server script in ServerScriptService.

That’s not always correct, anyone can define functions using : which provides the self variable. Functions defined like that are called using : and self would be the module. you’ve corrected that part with a reply, but don’t forget that he needs to change the function definition. Also:

not the function, the thing that contains a reference to the function

The colon makes the function a variable, which you can reference using “self”. You’d want to use the dot in this situation.

What is a function definition? I’m still a little bit new to making functions in modules

We say that we have defined a function when we give it its code. We say that we have declared a function when we give it its name but we can’t do that in Lua (so in Lua, a function declaration is a function definition).

The problem with this is that you’re requiring the module every time the character is added. You’re only supposed to require a module script once at the beginning of your program.

Also to answer your question:

You use a colon instead of a period for OOP. For example, let’s say you have a table with the given metatable:

local t = {}
local meta = {}
meta.__index = t

function t.new(name: string?)
    local self = setmetatable({}, meta)
    self.Name = name or ""
    return self
end

function t:SayName()
    --// since we're using a colon, we can use the "self" keyword
    --// to reference the table that the function is being called from
    --// as the first parameter
    print(self.Name)
end

local newObject = t.new("super object")
--// this:
newObject:SayName()
--// is the same as:
t.SayName(newObject)