Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my script say " Argument 2 missing or nil" in my Data Store script?

I have created a Data Store script but when i click Play it does not work and always says "Argument 2 missing or nil"

local DS = game:GetService("DataStoreService")
local myDS = DS:GetDataStore("test")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name  = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = myDS:setAsync(player.UserId.."-cash")
	end)
	if success then
		cash.Value = data
	else 
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDS:SetAsync(player.UserId.."-cash", player.leaderstats.cash.Value)
	end)
	if success then
		print("Player Data success saved!")
	else
		print("There was an error when saving data")
		warn(errormessage)
	end
end)

4 comments

2

The method :SetAsync() is to be used only for writing* data, it requires two arguments: The Data-Key, and the Data to be written: on line 15 you're missing this argument.

1

Regardless, you're calling the incorrect function. You're looking for :GetAsync().

1

You shouldn't be using :SetAsync() too much either, as it overrides any previous data, if your Cash value is an integer, :IncrementAsync() is something you should take a look at.

0

@Feahen Thank you so much!

1 answer

✓ Accepted answer

Like the person said in the comments, SetAsync requires 2 arguments and you gave it only one

i think you ment GetAsync instead and just made a typo on line 15

This discussion is preserved as it was. New answers, comments, and votes are closed.