Hi, this datastore script is not loading and saving data correctly
It always says in the console: failed to load data of the player
local DataStoreService = game:GetService("DataStoreService")
local CashDataBase = DataStoreService:GetDataStore("CashData")
local MissionsDataBase = DataStoreService:GetDataStore("MissionsData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local PlayerData = Instance.new("Folder")
PlayerData.Name = "PlayerData"
PlayerData.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
local MissionsData = Instance.new("IntValue")
MissionsData.Name = "MissionsData"
MissionsData.Value = 0
MissionsData.Parent = PlayerData
local playercash
local playermissions
local success, result = pcall(function()
playercash = CashDataBase:GetAsync(player.Userid)
end)
local success2, result2 = pcall(function()
playermissions = MissionsDataBase:GetAsync(player.Userid)
end)
if success then
Cash.Value = playercash
else
warn("There was a error loading the cash data of the player: " .. player.UserId)
end
if success2 then
MissionsData.Value = playermissions
else
warn("There was a error loading the missions data of the player: " .. player.UserId)
end
game.Players.PlayerRemoving:Connect(function(player)
local success, result = pcall(function()
return CashDataBase:SetAsync(player.UserId, Cash.Value)
end)
local success2, result2 = pcall(function()
return MissionsDataBase:SetAsync(player.UserId, MissionsData.Value)
end)
if success then
print("Successfully saved the cash data of the player: " .. player.UserId)
else
warn("There was a error saving the cash data of the player: " .. player.UserId)
end
if success2 then
print("Successfully saved the missions data of the player: " .. player.UserId)
else
warn("There was a error saving the missions data of the player: " .. player.UserId)
end
return success, success2
end)
end)