I cannot save my level value to data store, everytime i load the data, it gives nil value. Here's the code:
local DataStoreService = game:GetService("DataStoreService");
local PD = DataStoreService:GetDataStore("PlayerData");
function SaveData(player)
PD:SetAsync(player.usedId,player.PlayerData.Stats.Level.Value);
end
function LoadData(player)
local success,err = pcall(function()
PLD = PD:GetAsync(player.userId);
end)
if success then
if PLD ~= nil then
player.PlayerData.Stats.Level.Value = 1;
else
player.PlayerData.Stats.Level.Value = PLD;
end
else
player.PlayerData.Stats.Level.Value = 1;
end
end
game.Players.PlayerRemoving:Connect(function(plr)
repeat wait() until plr.PlayerData;
SaveData(plr);
end)
game.Players.PlayerAdded:Connect(function(plr)
repeat wait() until plr.PlayerData;
LoadData(plr);
end)
