DataStore overrides my values to 0?

It works, saves, and loads.
When I create a new piece of data to store, it overrides my value and sets it to 0.
How can I fix this?

local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local key = "key-"..player.userId
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	
	local StaminaMax = Instance.new("NumberValue", folder)
	StaminaMax.Name = "StaminaMax"
	StaminaMax.Value = 100
	
	local StrengthMax = Instance.new("NumberValue", folder)
	StrengthMax.Name = "StrengthMax"
	StrengthMax.Value = 100
	
	local StaminaAmount = Instance.new("NumberValue", StaminaMax)
	StaminaAmount.Name = 'StaminaAmount'
	StaminaAmount.Value = 100
	
	local StrengthAmount = Instance.new("NumberValue", StrengthMax)
	StrengthAmount.Name = 'StrengthAmount'
	StrengthAmount.Value = 100
			
	local Yen = Instance.new("NumberValue", folder)
	Yen.Name = "Yen"
	Yen.Value = 100
	
	local walkSpeed = Instance.new('NumberValue', folder)
	walkSpeed.Name = "Walkspeed"
	walkSpeed.Value = 16 	
	
	local save = DataStore:GetAsync(key)

	if save then
		StaminaMax.Value = save[1]
		StrengthMax.Value = save[2]
		Yen.Value = save[3]
		StaminaAmount.Value = save[4]
		StrengthAmount.Value = save[5]
		walkSpeed.Value = save[6]
	else
		local load = {StaminaMax.Value, StrengthMax.Value, Yen.Value, StaminaAmount.Value, StrengthAmount.Value, walkSpeed.Value}
		DataStore:SetAsync(key,load)	
	end

	while true do
		wait(5)
		Yen.Value = Yen.Value + 50
	end	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = "key-"..player.userId
	local load = {player.leaderstats:FindFirstChild("StaminaMax").Value, player.leaderstats:FindFirstChild('StrengthMax').Value,
	player.leaderstats:FindFirstChild("Yen").Value, player.leaderstats.StaminaMax:FindFirstChild('StaminaAmount').Value,
	player.leaderstats.StrengthMax:FindFirstChild('StrengthAmount').Value, player.leaderstats:FindFirstChild('Walkspeed').Value}
	DataStore:SetAsync(key,load)
end)

I was about to post the exact same thing until I found this post. Since I seem to be having the same issue and I have no idea why its doing it

It’s a pretty old post but appreciate the fact that you made a post expressing a similar issue here. I’d wager that the issue expressed in this thread is different from what you may be experiencing though.

When working with DataStores you should be mindful about their limitations. It’s completely possible that your data isn’t saving or it’s getting overridden because of a faulty implementation. What does your current DataStore code look like that’s giving you problems?

1 Like

Yes I did realize this post was from a year ago. But I’m making a simulator game with a backpack system with a max value and I’m trying to save that. When you buy more storage if that makes sense.
And I can’t really show the code because I don’t know how to format code blocks.

You can format a code block by using the formatter tool in the post editor (the bar above the area where you type) or by using three backticks to format code between.

```lua
-- your code here
```

1 Like

Thanks! sorry I didn’t reply right away I was busy with stuff and didn’t see this but here is the code:

local datastoreService = game:GetService("DataStoreService")
local maxPizzaSave = datastoreService:GetDataStore("CapacitySave")

game.Players.PlayerAdded:Connect(function(player)
	local maxPizza = Instance.new("IntValue")
	maxPizza.Name = "MaxPizza"
	maxPizza.Parent = player
	maxPizza.Value = 25

	print("added capacity system")

	local data
	local success, errormessage = pcall(function()
		data = maxPizzaSave:GetAsync(player.UserId.."-maxpizza")
	end)

	if success then
		maxPizza.Value = data
	else
		print("An error occured when loading the capacity saving system")
		warn(errormessage)
	end

end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		maxPizzaSave:SetAsync(player.UserId.."-maxpizza",player.MaxPizza.Value)
	end)

	if success then
		print("players max pizza saved successfully")
	else
		print("there was an error when saving the max pizza")
		warn(errormessage)
	end
end)