Datastorage problemo

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

1 Like

Sometimes network issues happen or just that you sent too many requests in 60 seconds its sad

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.

ok ill try to add this to see if it works

that sounds kinda rough im the only 1 in my game wouldnt imagine 10+ people lol

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.

Anyways, you should also try using UpdateAsync()

– 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?

1 Like

It can happen if u are constantly clicking run over and over

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

data as a value plus and negative signs?? sheeeeeeeesh this the new tech people use nowadays sign me up

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

ah i c i c now it makes more sense thanks for clearing it up

1 Like
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)