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?
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.
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()