DataStore sets value to 0?

This datastore makes any NEW value I insert and puts it to 0.

Also, whenever I CHANGE the value it keeps the saved one, instead of changing it… Why?

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 = 1
			
	local Yen = Instance.new("NumberValue", folder)
	Yen.Name = "Yen"
	Yen.Value = 100
	
	local walkSpeed = Instance.new('NumberValue', folder)
	walkSpeed.Name = "Walkspeed"
	walkSpeed.Value = 16
	
	---//Base Stats\\---
	local baseDamage = Instance.new("NumberValue", player)
	baseDamage.Name = 'BaseDamage'
	baseDamage.Value = 5
	local damage = Instance.new("NumberValue", player)
	damage.Name = 'Damage'
	damage.Value = baseDamage.Value * StrengthAmount.Value
	---//Base Stats\\---
	
	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

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)

Honestly, I have no idea if there’s anything wrong except for my heavy drowsiness.
However, I can point out some stuff.

1. It’s UserId, not userId

This is the only thing I can see causing errors here. I’m pretty sure as I have confirmed this.

2. FindFirstChid doesn’t guarantee a return value

FindFirstChild doesn’t guarantee a return value. That means that in this part of the code;

The values in the table are not guaranteed to be all available. FindFirstChild may or may not return nil values, which you only know by checking yourself manually if they exist. In this context, errors may be presented by the compiler, but since you’re exiting the game, you can’t see them.
Try intentionally saving data, and when an error shows up, you’ll know what to do.

If any of these don’t work, feel free to drop a reply. I’ll look into it when I wake up.

1 Like
	local key = "key-"..player.UserId
	local load = {player.leaderstats.StaminaMax.Value, player.leaderstats.StrengthMax.Value,
	player.leaderstats.Yen.Value, player.leaderstats.StaminaMax.StaminaAmount.Value,
	player.leaderstats.StrengthMax.StrengthAmount.Value, player.leaderstats.Walkspeed.Value}
	print('before')
	DataStore:SetAsync(key,load)
	print('after')

Edited version, one thing I’ve noticed, is it does NOT print after.

If you’re going to make DataStores, I recommend using DataStore2. It’s easier to use, plus backups and caching. You can find it here.

Try using pcall. Like, in this thread, the person who gave the solution explained it fully.

How to use pcall()? - #4 by JohnnyMorganz