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)
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)
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)
local MyStreak
DailyDatastore:UpdateAsync(Player.UserId, function(oldValue)
oldValue = oldValue or {}
MyStreak = oldValue.Streak or 1 -- basically what you did before but simpler