How to get an OOP object from another script?

I’m currently learning OOP, and I have an issue: How do I get an already existing object from another script? Storing them somewhere returns an empty table in other scripts (even in the same client-server boundary), and trying to get it through bindableevent returns a “tables cannot be cyclic” error.

2 Likes

I usually require the corresponding module and have a function that returns an object or a table i can access which stores objects.

Can you show me how you are doing it?

Also are you using Actors?

Get method:

local Character = require(game.ReplicatedStorage.Modules.BuildTypes.CharacterCreator).new(PlayerModel, StatsList)
StatsList.Character = Character

BindableEvent method:

local Char = Character.Misc.GetCharacter:Invoke()
PlayerModel.Misc.GetCharacter.OnInvoke = function()
	return Character
end

No, I dont use Actors (I dont even know what do they do tbh).

1 Like

Where do you mean by “somewhere”?

1 Like

I believe you cannot gain access to a specific Instance of an OOP class from another script. That is, if I have a “Dog” class in a module script, and in Script1, I make an instance of the Dog class as “Jerry”, I can’t grab Jerry from a different script.

1 Like

Another (or same as OOP one) module script

Create a table in the class, and when you create a new object, just add the object to the table. You should having some sort of identifier such as IDs or names for each object to be able to get a specific object you need.

Create a global module that handles creating OOP objects, and then store them in a key-table. then from any other script you can just require this module and do module.GetObject() or something

Yuh, this works. For some reason, Roblox’s Command Bar still cant see changes and returns an empty table, though.

I could be wrong about this as I haven’t used bindable events for a long time

bindable events don’t send tables by reference but rather they clone the table and send it
which means that [which makes the object in first script different from the object of the second script]

Obj A → (send through bindable event) → (bindable event makes a copy of Obj A (Obj B)) → Obj B is sent to the other script

if you want to send the object through an event you can use a signal module like good signal otherwise you can do what the others have said

Oh, GoodSignal can send metatables?

yes i think signal modules send tables by reference which means that they would be the same table in 2 scripts

local tbl = {}
tbl.A = 5

setmetatable(tbl, {__index = function()
	return "There Is A Meta Table"
end,})

local function test(t)
	print(t.A)
	print(t.IdkAnyThing) -- prints "There Is A Meta Table" because t is the same table as tbl
end

test(tbl)
1 Like

robloxs command bar doesnt store data, every time you execute a command its like youre pressing play again. Ive also made the same mistake :slight_smile:

Oh, I did not know that. Thanks for telling, that thing was causing Error 404 in my brain :smile:.

They do temporarily store data - each execution is more like calling a different function/scope because they cannot access local variables from a different execution. If you ommit the local term then it makes a global variable which exists until you close the game or switch between testing and editing. Observe:

Make a local variable a
Access it and get nil
Make global variable b
Access it and it works!

1 Like

Because it’s not the same execution context, there’s client, server, & command bar

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