Data not saving

Basically sometimes when a player rejoins there data gets wiped and i dont know why, here is the code

local Players = game:GetService(“Players”);
local DataStoreService = game:GetService(“DataStoreService”);

local SessionId = game.JobId;

local DataService = {};

local GameDataStore;

local function DeepCopy(data)
local CopyData = {};

for index,value in pairs(data) do
if type(value) == “table” then
CopyData[index] = DeepCopy(value);
else
CopyData[index] = value;
end
end
return CopyData;
end

function DataService:SetDataStore(key)
GameDataStore = DataStoreService:GetDataStore(key);
end

function DataService:GetData(key)
return GameDataStore:UpdateAsync(key, function(data)
if (not data) then
return;
end

data = DeepCopy(data);

if (not data.SessionId) then
	data.SessionId = SessionId;
	return data;
end

end)
end

function DataService:SetData(key, data)
return GameDataStore:UpdateAsync(key, function(olddata)
if (not olddata) then
return data;
end

if (olddata.SessionId == data.SessionId) then
	data.SessionId = nil;
	return data;
end

end)
end

return DataService;

2 Likes