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

Data Store script is giving the error "Argument 2 missing or nil"?

I'm trying to make a script that stores the value "Cash" in my script for a sword fighting game. I'm getting the error above on line 15 and I have no idea how to fix it since I got assistance from a YouTube tutorial on how to make this. If anyone could help that would be great.

local maxlevel = 25

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local level = leaderstats:WaitForChild("Level")
	local xp = leaderstats:WaitForChild("XP")
		
	while wait() do
		local levelvalue = level.Value
		local xpneeded = ((levelvalue * 2) - 1) * 50
		if level.Value < maxlevel then
			if xp.Value >= xpneeded then
				level.Value = level.Value + 1
				xp.Value = xp.Value - xpneeded
			end
		end
	end
end)

3 comments

0

when you SetAsync, it takes a key as the first parameter and a variant as the second parameter

0

do i not have that?

0

One of your errors is having a period instead of a comma separating those 2 arguments in each SetAsync

1 answer

✓ Accepted answer

Use this if you are making a save cash

Script in workspace

local DataStore = game:GetService("DataStoreService"):GetDataStore("greatneil80")


game.Players.PlayerAdded:connect(function(player)
	local plr = game.Players.LocalPlayer
	
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	
	local Gold = Instance.new("IntValue", stats)
	Gold.Name = "Cash"
	local key = "player-"..player.userId
	
	
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		--Save format: {points, coins}
		Gold.Value = savedValues[1]
	else
		local valuesToSave = {Gold.Value}
		DataStore:SetAsync(key, valuesToSave)
	end

	
end)

then when player leaves... new script in workspace.....

local DataStore = game:GetService("DataStoreService"):GetDataStore("greatneil80")

game.Players.PlayerRemoving:connect(function(player)
	
	local key = "player-"..player.userId
	
	--Save key: {points, coins}
	local valuesToSave = {player.leaderstats.Cash.Value}
	DataStore:SetAsync(key, valuesToSave)
	
end)

This should work... Have fun..

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