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

Getting error "Argument 2 is missing or nil" from Datastore script?

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
shared.CookieCount = 0


game.Players.PlayerAdded:Connect(function(player)
	local Original
	local UFO
	local OOB
	local Bacon
	local Sandstone
	local Burnt
	local Cactus
	local CF
	local Tiny
	
	local success, errormessage = pcall(function()
		Original = myDataStore:GetAsync(player.UserId.."-OriginalCookie")
		UFO = myDataStore:GetAsync(player.UserId.."-UFOCookie")
		OOB = myDataStore:GetAsync(player.UserId.."-OOBCookie")
		Bacon = myDataStore:GetAsync(player.UserId.."-BaconCookie")
		Sandstone = myDataStore:GetAsync(player.UserId.."-SandstoneCookie")
		Burnt = myDataStore:GetAsync(player.UserId.."-BurntCookie")
		Cactus = myDataStore:GetAsync(player.UserId.."-CactusCookie")
		CF = myDataStore:GetAsync(player.UserId.."-CFCookie")
		Tiny = myDataStore:GetAsync(player.UserId.."-TinyCookie")
	end)
	if success then
		shared.OriginalCookie = Original
		shared.UFOCookie = UFO
		shared.OOBCookie = OOB
		shared.BaconCookie = Bacon
		shared.SandstoneCookie = Sandstone
		shared.BurntCookie = Burnt
		shared.CactusCookie = Cactus
		shared.CFCookie = CF
		shared.TinyCookie = Tiny
		if shared.OriginalCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.UFOCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.OOBCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.BaconCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.SandstoneCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.BurntCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.CactusCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		elseif shared.CFCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
		if shared.TinyCookie then
			shared.CookieCount = shared.CookieCount+1
			wait()
		end
	else
		warn(errormessage)
		if Original == nil then
			shared.OriginalCookie = false
		end
		if UFO == nil then
			shared.UFOCookie = false
		end
		if OOB == nil then
			shared.OOBCookie = false
		end
		if Bacon == nil then
			shared.BaconCookie = false
		end
		if Sandstone == nil then
			shared.SandstoneCookie = false
		end
		if Burnt == nil then
			shared.BurntCookie = false
		end
		if Cactus == nil then
			shared.CactusCookie = false
		end
		if CF == nil then
			shared.CFCookie = false
		end
		if Tiny == nil then
			shared.TinyCookie = false
		end
		
	end

end)




game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-OriginalCookie",shared.OriginalCookie)
		myDataStore:SetAsync(player.UserId.."-UFOCookie",shared.UFOCookie)
		myDataStore:SetAsync(player.UserId.."-OOBCookie",shared.OOBCookie)
		myDataStore:SetAsync(player.UserId.."-BaconCookie",shared.BaconCookie)
		myDataStore:SetAsync(player.UserId.."-SandstoneCookie",shared.SandstoneCookie)
		myDataStore:SetAsync(player.UserId.."-BurntCookie",shared.BurntCookie)
		myDataStore:SetAsync(player.UserId.."-CactusCookie",shared.CactusCookie)
		myDataStore:SetAsync(player.UserId.."-CFCookie",shared.CFCookie)
		myDataStore:SetAsync(player.UserId.."-TinyCookie",shared.TinyCookie)
	end)
	
	if success then
		print("Data saved!")
	else
		print("Data save error.")
		warn(errormessage)
	end
	
end)

My problem is when the player leaves the game. The TinyCookie and CactusCookie variables are not saved, and when I leave the game I get warned with "Argument 2 missing or nil" from the warn on line 127. Can I get some help?

2 comments

0

The .."-CookieType" is used so I can save multiple values to the UserID, the comma comes after that, with the variable that I want to change.

0

o I see

1 answer

Okay, the error message it's self says it all.. The SetAsync() method takes 2 arguments, a datastore key and the data to save, the data cannot be nil.. now since your getting the values to save from shared, it indicates that one(if not multiple) properties that your reading from it are nil, and therefore causing the error.. so just to make sure all the values your looking for from the shared table are not nil

just a suggestion, since your data relates together, you can just pack it into a table and save the table into datastore.. also as a good practice, i recommend using the player's userId as the datastore scope, this way so your data is all organized and easy to maintain..

like this:

local Players = game:GetService("Players");
local DataStoreService = game:GetService("DataStoreService");

local gameDSKey = "mywonderfullgame123";

Players.PlayerAdded:Connect(function(player)
	local playeDataStore= game:GetDataStore(gameDSKey, player.UserId); -- datastore folder dedicated to just a single player;

	local playerStats

	local s, e = pcall(function()
		playerStats = playerDataStore:GetAsync("stats");
	end)

	if(s) then
		print("No errors");
		if(playerStats) then
			print("player is not new");
			print("The player has ", playerData.BurntCookies, " burnt cookies")
		else
			print("player is new");
		end
	else
		print(e)
	end
end)


Players.PlayerRemoving:Connect(function(player)
	local playerDataStore = game:GetDataStore(gameDSKey, player.UserId);
	local data  = {
		BurntCookies = 20,
		FreshCookies = 40;
		-- ... more data here
	};

	local s, e = pcall(function()
		playerDataStore:SetAsync("stats", data); --saves the table in datastore
	end)
end)

notice how much easier and shorter the above is?

this is even alot enhanced by wrapping the datastore code in a module script.. good luck

1 comment

0

I know for a fact that every shared value is true, but it still says argument 2 is missing or nil.

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