BIND CLOSE DONT WORK
I experiment and ik how data store loss and happen if you only use player removing if they accidentaly leave they can literally loss there data.
local DataStoreService = game:GetService("DataStoreService") -- DataStore Variable
local mysDataStore3 = DataStoreService:GetDataStore("mysDataStore3006") -- Variable for saving!
game.Players.PlayerAdded:Connect(function(player) -- PLAYER IF JOINS
local leaderstats = Instance.new("Folder",player) -- INSATNCE
leaderstats.Name = "statss" -- NAME
local Coins = Instance.new("IntValue", leaderstats) -- ISTANCE FOR CASH
Coins.Name = "Saved" -- NAME
Coins.Value = 0 -- AMOUNT
local playerUserId = "Player_"..player.UserId -- User
--Load Data
local data
local sucess, errormessage = pcall(function() -- if sucess or if not sucess
wait(0.05)
data = mysDataStore3:GetAsync(playerUserId) -- SyncData
end)
Coins.Value = data
if sucess then -- if sucess then
wait(0.05)
Coins.Value = data
else
wait(0.05)
Coins.Value = data
end
end)
local function Set(player)
wait(0.05)
local playerUserId = "Player_"..player.UserId
local data = player.statss.Saved.Value
local sucess, errormessage = pcall(function()
wait(0.05)
mysDataStore3:UpdateAsync(playerUserId, function(oldValue)
local newValue = data or oldValue or 0
return newValue
end)
end)
if sucess then
wait(0.05)
else
end
end
game.Players.PlayerRemoving:Connect(Set)
game:BindToClose(function()
for i, v in next, game.Players:GetChildren() do
if v then
Set(v)
end
end
end)
Can someody help me to figure it out why it dont work?