No matter how I try to call :Release(), it errors saying its not apart of the table, and data refuses to load.
PlayerAdded function:
function Data.PlayerJoined(player)
warn("LOADING")
if Data.Profiles[player.UserId] then
Data.Profiles[player.UserId]:Release();
player:Kick("Your data is currently being locked in session: " .. game.JobId .. ", please rejoin.");
return;
end
local profile = DataStore:LoadProfileAsync(tostring(player.UserId), false, "Repeat");
local self = {};
if profile and player then
warn("Loading 1/4")
if player.Parent then
warn("Loading 2/4")
profile:AddUserId(player.UserId);
profile:Reconcile();
profile:ListenToRelease(function()
player:Kick("Your data is released.");
Data.Profiles[player.UserId] = nil;
end)
if Data.Profiles[player.UserId] then
Data.Profiles[player.UserId]:Release();
player:Kick("Your data was already loadred in this session. Please rejoin.");
else
warn(profile)
warn("Loading 3/4")
self = setmetatable(profile, Data);
Data.Profiles[player.UserId] = self;
warn("Loaded 4/4");
return (self);
end
end
end
return self;
end
PlayerRemoving:
-- Stored in another script ~ shouldn't be a problem.
game.Players.PlayerRemoving:Connect(function(player)
if rakeData.Profiles[player.UserId] then
rakeData.Profiles[player.UserId]:Release();
end
end)
I’m setting it and using it later on for other functions… what do you mean by metamethods? I’ve tried setting ._index and all, and I’ve seen similar implementations work just fine.
That’s not how OOP works. In this situation, it doesn’t look like you are using it as player joined is just a standalone function. As said, try just setting the reference of self to the profile instead.
Regardless, that doesn’t answer my request. Yes, that could fix it, but that wouldn’t explain why it’s not working in this case. Your suggestion is also going to require me rewriting a lot of functions because every function to change, or get data, is stored in that metatable.
I think the problem here is that you setting all indexes from profile to go to another table which is data. It’s hard to see what the root cause is if I can’t see your meta method logic.
Hmm… this isn’t the issue. I’ve seen similar systems use the exact same logic. The only difference is I have a few functions on data. Like these:
function Data:removeItem(name)
for i, v in pairs(self.Data.Items) do
if v.Amount <= 0 and v.Name == name then
table.remove(self.Data.Items, i);
return;
end
end
return;
end
function Data:AddItem(name, amount)
local itemData = items[name];
for i, v in pairs(self.Data.Items) do
if v.Name == name then
v.Amount += amount;
self:removeItem(name);
return;
end
end
table.insert(self.Data.Items, {
Name = name,
ID = itemData.ID,
Amount = amount,
Locked = false;
})
return
end