GetAsync() returns nil

So I am building a controls menu for my game in which the player is able to edit the keys that some actions within the game are binded to (for instance, running or toggling a flashlight.)

So far everything has been going well, except for the data saving part of it.

In accordance with the official documentation of Roblox, I have created the following script:

game.Players.PlayerRemoving:Connect(function(player)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PlayerGold")

	-- Data store key and value

	local playerFlashlightButton = player:WaitForChild("PlayerGui").ControlMenu.FlashlightButton.Text
	local playerRunningButton = player.PlayerGui.ControlMenu.RunButton.Text

	-- Set data store key
	local setSuccess, errorMessage = pcall(function()
		goldStore:SetAsync(tostring(player.UserId), playerFlashlightButton, playerRunningButton)
	end)

	if not setSuccess then
		warn(errorMessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local DataStoreService = game:GetService("DataStoreService")
	local goldStore = DataStoreService:GetDataStore("PlayerGold")
	
	-- Get the data
	local getSuccess, currentGold = pcall(function()
		return goldStore:GetAsync(tostring(player.UserId))
	end)
	if getSuccess then
		print(currentGold)
		-- Print the data
	end
end)

The problem is that one of the very last lines of the code, the one where the data is printed, returns “nil” in output. I have already tried swapping out “currentGold” for “goldStore,” but it has just returned the same result. If anyone here knows how to solve this problem, please tell me. Thank you.

1 Like

Is API Services turned on? (CHAR)

1 Like

Yes. I’m assuming that you mean the API service setting in the “Security” tab.

2 Likes

Yes. I mean that. (CHARRRRRRRRRRRRRRR )

Am I not getting something when you say “CHARRRRRRR?”

The third argument in :SetAsync should be an array of UserIds, it’s going to error because you’re trying to set a string value in place of a table.

If you’re trying to set multiple values to a single key, you should be using tables, but if you’re just testing things out, you just have to remove this third argument.

1 Like

The annoying character limit.
image

1 Like

It’s to get past the character limit

1 Like

Oh, thank you! I didn’t know that there was a third seperate argument to the function.

1 Like