Greetings people, I've got a serious problem with data storing. It seems that, although the pcall says it was right, the player's data doesn't recover when he rejoins. Is there a reason why this happens?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerStat2019")
local MarketplaceService = game:GetService("MarketplaceService")
Players.PlayerAdded:Connect(function(Player)
local Leaderboard = Instance.new("Folder",Player)
Leaderboard.Name = "leaderstats"
local Wins = Instance.new("IntValue",Leaderboard)
Wins.Name = "Wins"
Wins.Value = 0
local Coins = Instance.new("IntValue",Player)
Coins.Name = "Coins"
Coins.Value = 0
local XP = Instance.new("IntValue",Player)
XP.Name = "XP"
XP.Value = 0
local Time = Instance.new("IntValue",Player)
Time.Name = "Time"
Time.Value = 0
local Level = Instance.new("IntValue",Leaderboard)
Level.Name = "Level"
Level.Value = 1
--// Tools
local BoughtTools = Instance.new("Folder",Player)
BoughtTools.Name = "BoughtTools"
--//Chatting and Gamepasses
local VIPGamepass = 7622135
local MegaVIPGamepass = 7622108
local BanHammerGamepass = 7622101
local HasVIP
local HasMegaVIP
local HasBanHammer
local Success,Error = pcall(function()
HasVIP = MarketplaceService:UserOwnsGamePassAsync(Player.UserId,VIPGamepass)
HasMegaVIP = MarketplaceService:UserOwnsGamePassAsync(Player.UserId,MegaVIPGamepass)
HasBanHammer = MarketplaceService:UserOwnsGamePassAsync(Player.UserId,BanHammerGamepass)
end)
if Success and HasBanHammer == true then
game.ServerStorage:WaitForChild("Tools").BanHammer:Clone().Parent = BoughtTools
end
if Success and HasMegaVIP == true then
if Player.Backpack:FindFirstChild("Boombox") == nil and Player.Character:FindFirstChild("Boombox") == nil then
game:GetService("ServerStorage"):WaitForChild("Tools").Boombox:Clone().Parent = Player.StarterGear
game:GetService("ServerStorage"):WaitForChild("Tools").Boombox:Clone().Parent = Player.Backpack
end
end
if Success and HasVIP == true then
if Player.Backpack:FindFirstChild("Boombox") == nil and Player.Character:FindFirstChild("Boombox") == nil then
game:GetService("ServerStorage"):WaitForChild("Tools").Boombox:Clone().Parent = Player.StarterGear
game:GetService("ServerStorage"):WaitForChild("Tools").Boombox:Clone().Parent = Player.Backpack
end
end
--// Data system
local Key = "Key_"..Player.UserId
local Data
local Success,Error = pcall(function()
Data = DataStore:GetAsync(Key)
end)
if Success then
if Data ~= nil then
Coins.Value = Data["Coins"]
Wins.Value = Data["Wins"]
XP.Value = Data["XP"]
Level.Value = Data["Level"]
print("Data of "..Player.Name.." successfully got!")
elseif Data == nil then
Data = {
["Coins"] = Player.Coins.Value;
["Wins"] = Player.leaderstats.Wins.Value;
["XP"] = Player.XP.Value;
["Level"] = Player.leaderstats.Level.Value;
["BoughtTools"] = {};
}
DataStore:SetAsync(Key,Data)
print("Data of "..Player.Name.." successfully got!")
end
else
error("Couldn't get data from DataStore: "..Error)
end
--// Game related script
Player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function()
if Character:FindFirstChild("GameTag") then
Character:FindFirstChild("GameTag"):Destroy()
end
Player:LoadCharacter()
end)
end)
end)
Players.PlayerRemoving:Connect(function(Player)
print("Debug")
local Key = "Key_"..Player.UserId
local Data = {
["Coins"] = Player.Coins.Value;
["Wins"] = Player.leaderstats.Wins.Value;
["XP"] = Player.XP.Value;
["Level"] = Player.leaderstats.Level.Value;
["BoughtTools"] = Player:WaitForChild("BoughtTools"):GetChildren();
}
local Success,Error = pcall(function()
DataStore:SetAsync(Key,Data)
end)
game:GetService("StarterGui"):SetCore("ChatMakeSystemMessage",{
Text = "[SYSTEM]: "..Player.Name.." left the game!",
Font = Enum.Font.SourceSans,
Color = Color3.new(255, 0, 0),
FontSize = Enum.FontSize.Size14
})
if Success then
print("Data of "..Player.Name.." successfully saved!")
else
error("Couldn't save data to DataStore: "..Error)
end
local Character = Player.Character or Player.CharacterAdded:Wait()
local Gametag = Character:FindFirstChild("GameTag")
Gametag:Destroy()
end)
game:BindToClose(function()
local PlayersLeft = Players:GetPlayers()
for i = 1, #Players do
local Player = Players[i]
local Key = "Key_"..Player.UserId
local Data = {
["Coins"] = Player.Coins.Value;
["Wins"] = Player.Wins.Value;
["XP"] = Player.XP.Value;
["Level"] = Player.leaderstats.Level.Value;
["BoughtTools"] = Player:WaitForChild("BoughtTools"):GetChildren();
}
local Success,Error = pcall(function()
DataStore:SetAsync(Key,Data)
end)
if Success then
print("Data of "..Player.Name.." successfully saved!")
else
error("Couldn't save data to DataStore: "..Error)
end
end
end)

2 comments
http://idownvotedbecau.se/toomuchcode . It is also counterintuitive to print the exception message as an exception, as the point of pcall is for exception handling. Consider printing the exception as a warning (with warn(), not error()), rather than as an exception.
stop posting virus links programmerHere