Okay, so maybe I wasn't clear enough last time I asked, so I got some silly answers. I will start by saying I understand how the DataStore works. So if there isn't a key, it returns nil.
Now my script won't even get that far if there isn't any data for the key in the store...
problem lies here: local playerData = FriendData:GetAsync(friendID) -----This line here is the problem... :/
local DataStore = {}
local DSS = game:GetService('DataStoreService')
local FriendData = DSS:GetDataStore('FriendData_Dev6')
local AUTOSAVE_INTERVAL = 300
local DS_RETRIES = 3
local MAX_FRIENDS = 20
local sessionData = {}
function DataStore.isPlayerLoaded(player)
return sessionData[player]
end
local function output(...)
print("[MOD_SERVER_FRIENDS]", ...)
end
function DataStore.getFriendCount(player)
return #sessionData[player]
end
function DataStore.areFriends(player, name)
local isFriend = false
output("Checking", player.Name, name.Name)
for i = 1, #sessionData[player] do
if sessionData[player][i]["Name"] == name.Name then
output(player.Name, name.Name, "Friends!")
isFriend = true
end
end
return isFriend
end
local function dataStoreRetry(dataStoreFunction)
local tries = 0
local success = true
local data = nil
repeat
tries = tries + 1
success = pcall(function() data = dataStoreFunction() end)
if not success then wait(1) end
until tries == DS_RETRIES or success
if not success then
error('Could not access DataStore! Warn players that their data might not get saved!')
end
output("DSR", success, data)
return success, data
end
local function getPlayerData(player)
return dataStoreRetry(function()
return FriendData:GetAsync(player.userId)
end)
end
local function getPlayerDataViaUserID(userID)
return dataStoreRetry(function()
return FriendData:GetAsync(userID)
end)
end
local function savePlayerData(player)
if sessionData[player] then
return dataStoreRetry(function()
return FriendData:SetAsync(player.userId, sessionData[player])
end)
end
end
local function setupPlayerData(player)
local success, data = getPlayerData(player)
if not success then
sessionData[player] = false
else
if not data then
sessionData[player] = {}
savePlayerData(player)
else
sessionData[player] = data
end
end
end
local function autosave()
while wait(AUTOSAVE_INTERVAL) do
for player, data in pairs(sessionData) do
game.ReplicatedStorage.CrossConnections.Loading.ShowIcon:FireClient(player, true)
savePlayerData(player)
game.ReplicatedStorage.CrossConnections.Loading.ShowIcon:FireClient(player, false)
end
end
end
function DataStore:GetOnlineFriends(player)
local onlineFriends = {}
if sessionData[player] ~= nil then
for i = 1, #sessionData[player] do
if sessionData[player][i]["Online"] then
output(sessionData[player][i]["Name"], "is online!")
table.insert(onlineFriends, #onlineFriends, sessionData[player][i]["Name"])
else
output(sessionData[player][i]["Name"], "is offline!")
end
end
end
return onlineFriends
end
function DataStore:UpdateStatus(player, isOnline)
output("Update")
if not DataStore.isPlayerLoaded(player) then
repeat wait() output("Waiting for Player:", player)
until DataStore.isPlayerLoaded(player)
else
output("Player is loaded.", player)
output("DataSize:", #sessionData[player])
end
for i = 1, #sessionData[player] do
local friendID = game.Players:GetUserIdFromNameAsync(sessionData[player][i]["Name"])
output("DEBUG", friendID)
if friendID ~= nil then
output(player, ":", sessionData[player][i]["Name"], "::", friendID)
end
local playerData = FriendData:GetAsync(friendID) -----This line here is the problem... :/
output("DEBUG", playerData)
if playerData ~= nil then
for i, v in pairs(playerData) do
output("DEBUG", i, v)
end
FriendData:SetAsync(game.Players:GetUserIdFromNameAsync(sessionData[player][i]["Name"], playerData))
end
end
end
function DataStore:AddFriend(player, newFriend)
output(player.Name, "added", newFriend.Name)
local isAlreadyFriend = false
if player.Name ~= newFriend.Name then
for i = 1, #sessionData[player] do
if sessionData[player][i]["Name"] == newFriend.Name then
isAlreadyFriend = true
end
end
if not isAlreadyFriend then
if #sessionData[player] < MAX_FRIENDS then
local newFriendTable = {Name = newFriend.Name, Online = true}
table.insert(sessionData[player], #sessionData[player], newFriendTable)
else
output(player.Name, "'s friends list is full.")
end
else
output(player.Name, "is already friends with", newFriend.Name)
end
else
output(player.Name, "can't add self")
end
end
function DataStore:RemoveFriend(player, oldFriend)
output(player.Name, "removed", oldFriend.Name)
for i = 1, #sessionData[player] do
if sessionData[player][i]["Name"] == oldFriend.Name then
table.remove(sessionData[player], i)
end
end
end
game.Players.PlayerAdded:connect(function(plr)
setupPlayerData(plr)
DataStore:UpdateStatus(plr, true)
end)
game.Players.PlayerRemoving:connect(function(plr)
DataStore:UpdateStatus(plr, false)
savePlayerData(plr)
end)
spawn(autosave)
return DataStore

4 comments
China called, they want their wall back.
Also there's clearly no data saved for friendId in that case...
I figured I'd give the whole thing so I didn't get childish answers. I'm fully aware of how the DataStore works, I also know how to follow my code. I'm just unaware as to why this doesn't work. :D
Well now you know why. Because there's no data saved.