Datastore not working as it should

Hello there! I’m trying to learn how to save data with DataStoreService, and I’m using the official Roblox guide to help me get stared. I followed along with it step by step, only replacing the value with an integer value, however something in the code is preventing it from working. The value I wanted to be saved and printed is not printed, and instead it displays the error message “Argument 2 missing or nil”. Can anyone help? (and yes, I did enable Studio Access for API Services)

local dataStoreService = game:GetService("DataStoreService")
local storedValue = dataStoreService:GetDataStore("storedValue")


game.Players.PlayerRemoving:Connect(function(player)
	local setSuccess, errorMessage = pcall(function()
		storedValue:SetAsync(player.UserId, player.Gold.Value)
	end)
	if setSuccess then
		print("Saved data")
	end
	if not setSuccess then
		warn(errorMessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Parent = player
	
	local data
	local setSuccess, errorMessage = pcall(function()
		data = storedValue:SetAsync(player.UserId)
	end)
	if setSuccess then
		gold.Value = data
		print(gold.Value)
	end
	if not setSuccess then
		warn(errorMessage)
	end
end)

Sorry if its a simple mistake I’m making, I have no idea what I’m doing.

The datastore method SetAsync() has two parameters: key, value
You wrote data = storedValue:SetAsync(player.UserId) and didn’t write the value.

This is supposed to be GetAsync(), not SetAsync()

1 Like

This made the error go away, however when I manually edit the integer’s value and save it it’s still set to 0 though.

nevermind you just can’t manually edit it.

1 Like

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