Value is nil though it shouldn't be

I Want to achieve that the value is no longer nil, the problem with this is that the check i’ve placed in for it does not work, i am saving that value inside a table which will get saved on the datastore if the player joins the game, even with the check does another part of the script error out as the value is nil, i don’t know what is causing this.


local Success,ErrorMessage = pcall(function()
	local MyStreak = CurrentDailyData.Streak -- `CurrentDailyData` is the datastore that gets called after the character of the player spawned into the game
	if MyStreak == nil then MyStreak = 1 end --The check i was talking about...
	
	local PrefTable = {
		LastCollect = os.time(),
		NextCollect = os.time()+60000,
		Streak = MyStreak,
	}
	DailyDatastore:SetAsync(Player.UserId,PrefTable)
end)

You should use UpdateAsync for this

local Success, ErrorMessage = pcall(function()
	DailyDataStore:UpdateAsync(player.UserId, function(oldValue)
	   MyStreak = oldValue.Streak or 1 -- basically what you did before but simpler 
	
	    local PrefTable = {
		    LastCollect = os.time(),
		    NextCollect = os.time()+60000,
		    Streak = MyStreak,
	    }

        print(MyStreak, PrefTable.Streak) --> see what it prints
        return PrefTable -- return the new value (updates the data store)
	end)
end)

try this and see if it works

Turns out i had misread the error message, apparently the datastore is nil, i don’t know if it can help, but this is the current code… (Error is still there)

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait()
	local Success,ErrorMessage = pcall(function()
		CurrentDailyData = DailyDatastore:GetAsync(Player.UserId)
	end)
	if not Success and CurrentDailyData ~= nil then
		warn("Error: "..ErrorMessage)
	end

	for i = CurrentSlot, AmountOfSlots do
		PickLoot(i,Player)
	end
	local LootObject = {LootObject = PickedLoot,LootStreak = CurrentDailyData.Streak} -- The error occures here
	DailyEvent:FireClient(Player,LootObject)
	
	local Success, ErrorMessage = pcall(function()
		local MyStreak
		DailyDatastore:UpdateAsync(Player.UserId, function(oldValue)
			MyStreak = oldValue.Streak or 1 -- basically what you did before but simpler 

			local PrefTable = {
				LastCollect = os.time(),
				NextCollect = os.time()+60000,
				Streak = MyStreak,
			}

			print(MyStreak, PrefTable.Streak) --> see what it prints
			return PrefTable -- return the new value (updates the data store)
		end)
	end)
	if not Success then
		warn("Error: "..ErrorMessage)
	end
end)

Error:
image

You don’t know if the player has data (first time joining). So just do:

MyStreak = oldValue and oldValue.Streak or 1

2 Likes
local MyStreak
DailyDatastore:UpdateAsync(Player.UserId, function(oldValue)
    oldValue = oldValue or {}
    MyStreak = oldValue.Streak or 1 -- basically what you did before but simpler 

You can do this or what @Ze_tsu suggested

Turns out the problem was that i was fetching the data where Streak was nil here

	local Success,ErrorMessage = pcall(function()
		CurrentDailyData = DailyDatastore:GetAsync(Player.UserId)
	end)

The problem was solved by replacing this

local LootObject = {LootObject = PickedLoot,LootStreak = CurrentDailyData.Streak}

With this:

local LootObject = {LootObject = PickedLoot,LootStreak = DailyDatastore:GetAsync(Player.UserId).Streak}

Still thanks for the help on this problem.