Not sure if this would be the case, but you’re saving individual values of data…? Can’t you just save them all inside a table rather than just calling SetAsync/GetAsync twice?
local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = 'Player_' .. player.UserId
local Data
local playerData = {
player:WaitForChild('leaderstats').Water.Value;
player:WaitForChild('leaderstats').Rebirths.Value
}
local succ, err = pcall(function()
Data = Datastore:GetAsync(playerUserId)
end)
if not succ then
warn(err)
else
playerData[1] = Data[1]
playerData[2] = Data[2]
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = 'Player_' .. player.UserId
local playerData = {
player:WaitForChild('leaderstats').Water.Value;
player:WaitForChild('leaderstats').Rebirths.Value
}
local succ, err = pcall(function()
Datastore:SetAsync(playerUserId, playerData)
end)
if succ then
print('saved!')
else
warn(err)
end
end)
I guess this is because the table is nil. After you quit the game, it saves. This means if you were new to the game, it couldn’t be found. So make it
if not succ then
warn(err)
else
if playerData ~= nil then
playerData[1] = Data[1]
playerData[2] = Data[2]
else
print("Player is new to the game.")
end
end
Try this and also make sure you are changing the value on the server not on the client
local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = 'Player_' .. player.UserId
local Data
--local playerData = {
-- player:WaitForChild('leaderstats').Water.Value;
-- player:WaitForChild('leaderstats').Rebirths.Value
--}
local succ, err = pcall(function()
Data = Datastore:GetAsync(playerUserId)
end)
if not succ then
warn(err)
else
player:WaitForChild('leaderstats').Water.Value = Data[1]
player:WaitForChild('leaderstats').Rebirths.Value = Data[2]
print("Data loaded")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = 'Player_' .. player.UserId
local playerData = {
player:WaitForChild('leaderstats').Water.Value;
player:WaitForChild('leaderstats').Rebirths.Value
}
local succ, err = pcall(function()
Datastore:SetAsync(playerUserId, playerData)
end)
if succ then
print('saved!')
else
warn(err)
end
end)
Then it would be better to use the BindToClose function because there is a very small chance of being saved in datastores. PlayerRemoving event won’t work consistently when the server shuts down.
BindToClose is used when the server is going to shut down. It is sometimes used to undertake some of the actions that can’t be used when the game is going to be closed. But not players when they crash.
local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = 'Player_' .. player.UserId
local WaterData, RebirthsData
local Water = player:WaitForChild('leaderstats').Water
local Rebirths = player:WaitForChild('leaderstats').Rebirths
local succ, err = pcall(function()
Data = Datastore:GetAsync(playerUserId)
end)
if succ then
if Data ~= nil then
Water.Value = Data.Water
Rebirths.Value = Data.Rebirths
else
Water.Value = 0 -- or some other default value
Rebirths.Value = 0 -- or some other default value
end
else
warn(err)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = 'Player_' .. player.UserId
local playerData = {
Water = player.leaderstats.Water.Value;
Rebirths = player.leaderstats.Rebirths.Value;
}
local succ, err = pcall(function()
Datastore:SetAsync(playerUserId, playerData)
end)
if succ then
print('saved!')
else
warn(err)
end
end)