My datastore has been failing to set the data to new users, making their values 0. This ruins the game for new players and makes it unplayable.
I am unsure of why this occurs. Any help appreciated.
local Players = game:GetService("Players")
local DST = game:GetService("DataStoreService")
local MoneyStore = DST:GetDataStore("MoneyStore2")
local MoneyValue
local plr
Players.PlayerAdded:Connect(function(Player)
plr = Player
wait()
MoneyValue = Instance.new("IntValue",Player)
MoneyValue.Name = "Bank"
local data
local success,erromessage = pcall(function()
data = MoneyStore:GetAsync(Player.UserId.."Money")
end)
if success then
if data ~= nil then
MoneyValue.Value = data
elseif data == nil then
MoneyValue.Value = 200
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local success, errormessage = pcall(function()
MoneyStore:SetAsync(Player.UserId.."Money",Player.Bank.Value)
end)
if success then
print("Data Saved")
else
print("Error while saving")
warn(errormessage)
end
end)