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.
local Player = {List = {}}
Player.__index = Player
function Player.getPlayerByName(name)
return Player.List[name]
end
function Player.new(name, id, idk)
local self = setmetatable({
Name = name;
Id = id;
Idk = idk;
}, Player)
Player.List[name] = self
return self
end
(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.
I have read it a few times, thanks
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.
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
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!