OOP question: How to index objects from outside that object

OK lets say I create an object for the player when they join, this object has some fun methods and then we have a part in the world with a click detector. That click detector will want to fire a method inside the player object.

I know the player object has a unique identity as a table, but how do i send the function from the click detector to that specific players object?

Sorry if this is too general, its a general concept I’m trying to wrap my head around in OOP in Roblox. When we create objects, they seem to be just floating around and while I have been able to create interesting behaviors inside the object, I am not sure how i can get objects to talk to each other.

I don’t understand the question you’re asking, but I’ll try to answer what I do understand.

You could create a table of all the objects that are assigned to players, as so

local Objects = {};

In your constructor function, you could add the newly created object to the Objects table, as so:

function Object.new()
       -- construct stuff
       Objects[self.Player] = self
       return self

Then you would just do Objects[aPlayerInstanceHere]:CoolMethod()


This is my personal solution:

local Player = {List = {}}
Player.__index = Player

function Player.getPlayerByName(name)
    return Player.List[name]

function Player.new(name, id, idk)
    local self = setmetatable({
        Name = name;
        Id = id;
        Idk = idk;
    }, Player)

    Player.List[name] = self    

    return self

(You also don’t have to put the list in the player table, you can make it a variable. The reason I did it was so you could access it from other scripts).


If you’re struggling with object orientated programming you should really check out this article by magnalite. It is quite old but still very relevant.

1 Like

I would like to point out that this article does not have anything that answers his specific question.

1 Like

I have read it a few times, thanks :slight_smile:
I am already up and running making classes and cool objects, just struggling how to make them talk to each other because I have seen an examples of it.

This is how I have been doing it so far, it seemed inelegant and I did get some critique in another thread, that I didn’t fully understand, that having to pass the player object each time was redundant.

I was thinking of creating a player object when a player joins then creating the other objects needed inside that player object.

Not sure.

I did not understand what he was asking at all, so I sent him the link after reading the following:

1 Like

You can pass the players name and store the players name as indexes, or UserId’s. It’s up to you.


I was wondering if this was what you were looking for?

1 Like

Well, my question was specifically is there some clever way to get objects to speak to each other and how to find them by “name” so they could be looked up and acted on. Lets say I have a class that creates a Gui object for a player and inside are some methods that are used to update the Gui and do other things.

Now I have a click detector out in the map, it is also an object and has values inside it. When the player clicks it, we have two unrelated objects trying to speak to each other. The click detector can trigger a method or function and pass argument such as the player who click, the object clicked, and any other values.

The heart of the question is, there are many player objects running around and I need to trigger that method in ONLY the player who clicked’s object. that table/object has a name and it exists, but it cant simply be called from thin air.

So i was storing all the player objects in a table upon creation, and using the UserID and the key. I had received something that felt like criticism for this way of doing things and so I made this thread, maybe there’s a better way? maybe bot?

I also really just want to thank everyone for taking time to look at my nonsense. I am new to OOP in Lua but for the most part I am adapting quickly, this one concept just had me stuck and I was looking for best practices.


I took a quick look at the other thread and it seems to me you are mistaken. You were criticized for the redundancy of the parameters on your methods and not the way in which you store your objects

1 Like

You are right, i was passing in (player) as an arg to the method when I was already using the method from the player table.

I’m just starting to wrap my head around this, i solved both threads with everyone’s patience and help. I am coming away from this with a better understanding of what I am doing and so even though both threads seem like a bit of a boondoggle, they have been SUPER helpful for me!

Thanks to all!

1 Like