How can I call trough a event OOP?

So Hello, I have this script: (Just for testing)

Is it possible to call OOP or current object and get it trough event?
SOO
IMPORTANT NOW

What I mean is, exchange of doing this:

local CharacterCreator = require(game.ReplicatedStorage.OOP2.Character)
local data = game:GetService("DataStoreService"):GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(plr)
	local character = plr.Character or plr.CharacterAdded:Wait()
	local PlayerCharacter = CharacterCreator.new(character)
	PlayerCharacter:AddCash(100)
	print(PlayerCharacter.cash)
	local success,Data = pcall(function()
		return data:GetAsync(plr.UserId)
	end)
	if Data then
		print("Data")
		PlayerCharacter:AddCash(data[1])
		print(data[1])
	end
	game.Players.PlayerRemoving:Connect(function()
		local cash = PlayerCharacter:ReturnCash()
		data:SetAsync(plr.UserId, cash)
	end)
end)

MODULE:

local Character = {}
Character.__index = Character

function Character.new(CreateCharacter)
	print("Create metatable.")
	return setmetatable({Character = CreateCharacter, name = CreateCharacter.Name, cash = 0}, Character)
end

function Character:AddCash(Amount)
	self.cash = self.cash + Amount
	print(self.cash)

end

function Character:ReturnCash()
	return self.cash
end


return Character

Everything on player event, is there a way to get the PlayerCharacter created before,to call it on a event exchange of callint it all on PlayerAdded event?

you have RemoteEvent in the Replicated Storage?

Yes, but, I just want to know if we can call the SELF player, on a event

I don’t see how it would be possible with a remote event to get the self instance because it’s unique to the specific object you created. It would maybe be possible if you saved all the objects somewhere and then called a remote event to grab a specific object from the list or something similar.

What I mean, is like, if there’s way to make like:

game.ReplicatedStorage.Event.OnServerEvent(plr)
CharacterCreator:AddCash(20) -
end)

That will obviously not work, because, it’s the character creator and not the metatable, so , how can I make it or it’s impossible

You could store the individual Character objects in a dictionary with the Keys being the Player UserIds.
Then you can use the Character objects in other events:

local CharacterObjects = {}

--// In the PlayerAdded
CharacterObjects[Player.UserId] = PlayerCharacter

--// In the PlayerRemoving
local Cash = CharacterObjects[Player.UserId]:ReturnCash()
2 Likes