Why isn`t it saving both values?

Why is it only saving keys or meat and not both values?


local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("LeaderStatSave")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local keys = Instance.new("IntValue")
	keys.Name = "Keys"
	keys.Parent = leaderstats
	
	local Meat = Instance.new("IntValue")
	Meat.Name = "Meat"
	Meat.Parent = leaderstats
	
	local data 
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-Keys")
	end)
	
	local data2
	local success, errormessage = pcall(function()
		data2 = playerData:GetAsync(player.UserId.."-Meat")
	end)
	
	if success then 
		keys.Value = data
		Meat.Value = data2
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-Keys", player.leaderstats.Keys.Value)
		playerData:SetAsync(player.UserId.."-Meat", player.leaderstats.Meat.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)

I mean, you used GetAsync() in two different pcalls, but then you used SetAsync() in one pcall. Is that maybe an oversight?

I don’t know, it is printing Data successfuly saved so it sets the async, but it seems to don’t load it right, because when I rejoin I am only having Meat.

1 Like

Maybe try this, because it sounds like a read issue, and you’re overwriting success and errormessage before you check them:

	local data = 0
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-Keys")
	end)

	if success then 
		keys.Value = data
	else
		print("Error while getting your data (keys)")
		warn(errormessage)
	end
	
	local data2 = 0
	local success, errormessage = pcall(function()
		data2 = playerData:GetAsync(player.UserId.."-Meat")
	end)
	
	if success then
		Meat.Value = data2
	else
		print("Error while getting your data (meat)")
		warn(errormessage)
	end
2 Likes

You have two variable called : “success” change it

2 Likes

Thank you that worked fine, it saved and loaded :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.