local DataStorageService = game:GetService("DataStoreService")
local NonPvPStorage = DataStorageService:GetDataStore("NonPvPStorage")
-- load data
game.Players.PlayerAdded:Connect(function(player)
local NonPvPStats = Instance.new("Folder")
NonPvPStats.Name = "NonPvPStats"
NonPvPStats.Parent = player
local Hunger = Instance.new("IntValue")
Hunger.Name = "Hunger"
Hunger.Value = 300
Hunger.Parent = NonPvPStats
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = NonPvPStats
local Health = Instance.new("NumberValue")
Health.Name = "Health"
Health.Parent = NonPvPStats
local NonPvPData
local Success , Fail = pcall(function()
NonPvPData = NonPvPStorage:GetAsync(player.UserId)
end)
if NonPvPData then
if Success then
Hunger.Value = NonPvPData.Hunger
Money.Value = NonPvPData.Money
Health.Value = NonPvPData.Health
else
player:Kick("Data failed to load so you got kicked")
end
end
end)
-- save data
game.Players.PlayerRemoving:Connect(function(player)
local Success , Fail = pcall(function()
local NonPvPTable = {
Hunger = player.NonPvPStats.Hunger.Value;
Money = player.NonPvPStats.Money.Value;
Health = player.NonPvPStats.Health.Value;
}
NonPvPStorage:SetAsync(player.UserId, NonPvPTable)
end)
if Success then
print("NonPvPStats Saved")
else
print("NonPvPStats Failed to save")
end
end)
sometimes my datastore saves and sometimes it dosnt is there a certain reason and how would i fix / inprove it i do not like doing datastorage stuff
What’s likely happening is that the server is shutting down before the data has saved, I would look into implementing the game:BindToClose callback to ensure that the player data is saved before the server shuts down.
I love the typo on the title “problemo”. Lol.
Also yeah you kicked the player if they’re data was not retrieved, so try having something on player removing so that it doesn’t save if it was because of that. Like you could create a value inside of the player, that doesn’t let the player attempt to save. Because then, they would be saving NO DATA. And over writing their old data with empty data.
– save data 2
game:BindToClose(function()
if not RunService:IsStudio() then
local players = game:GetPlayers()
for i,player in pairs(players) do
local Success , Fail = pcall(function()
local NonPvPTable = {
Hunger = player.NonPvPStats.Hunger.Value;
Money = player.NonPvPStats.Money.Value;
Health = player.NonPvPStats.Health.Value;
}
NonPvPStorage:SetAsync(player.UserId, NonPvPTable)
end)
if Success then
print("NonPvPStats Saved 2")
else
warn(Fail)
end
end
end
end)
idk how i would check if the player.removing already saved the data so i saved it again
would this work?
I usually save player data like this, however I often see people using your approach as well.
local dataSaving = 0
Players.PlayerRemoving:Connect(function()
dataSaving += 1;
-- Save the player data here using SetAsync
dataSaving -= 1;
end)
game:BindToClose(function()
while dataSaving > 0 do
game:GetService('RunService').Heartbeat:Wait()
end
end)```
I haven't had any issues using this approach for saving player data
Oh wait, I just realized that what I wrote was not completely valid Lua… I accidentally sprinkled a bit of JavaScript syntactic sugar in there haha, I’ll edit that real quick
local Success , Result = pcall(NonPvPStorage.GetAsync , NonPvPStorage , player.UserId)
if Success then
Hunger.Value = NonPvPStorage.NonPvPData.Hunger
Money.Value = NonPvPStorage.NonPvPData.Money
Health.Value = NonPvPStorage.NonPvPData.Health
print("NonPvPStorage Data Loaded")
else
print("NonPvPStorage Data Failed to load")
end
end)
NonPvPData is not a valid member of GlobalDataStore "DataStoreService.NonPvPStorage"
how would i get the table from
local NonPvPData = {
Hunger = player.NonPvPStats.Hunger.Value;
Money = player.NonPvPStats.Money.Value;
Health = player.NonPvPStats.Health.Value;
}
local Success , Result = pcall(NonPvPStorage.SetAsync, NonPvPStorage, player.UserId, NonPvPData)