Hello!
I am currently making a game that heavily revolves around DataStores. One of the game’s features is that you can “follow” players to get their latest stats and things like that. As done with all similar player-based features in my game, I’m using DataStores. The only problem is that when the local script inside the UI (Which is where you can see the user’s latest stats and follow them) invokes a function to retrieve (but also used to save) data, it will return the default table:
local defaultTab = {
["Following"] = {},
["Followers"] = {}
}
The only problem is that it doesn’t return along with the table the user’s followers and other users they follow. A table of a user who has followers and are also following other people should’ve looked like this:
local userTab = {
["Followers"] = { -- User ids from players that follows them
[8271939637] = true,
[8423248723] = true,
[1236123948] = true
},
["Following"] = { -- User ids from players they follow
[8173432734] = true,
[9128236734] = true,
[2187432748] = true
}
}
Every time they follow or unfollow a player, the script linked to the function uses UpdateAsync() to update the users they follow or don’t follow. Same thing when someone follows them.
Here is the code from the script that saves the user’s followers when followed:
(The “target” variable is supposed to be the UserId of whoever they wish to follow.)
function followFunction(plr, target, action)
if action == "follow" then
if plr.UserId == target then return end
local localAlreadyHasData
local targetAlreadyHasData
local localUniqueKey = plr.UserId.. "-FollowData"
local targetUniqueKey = target.. "-FollowData"
local localData = FollowDataStore:GetAsync(localUniqueKey)
local targetData = FollowDataStore:GetAsync(targetUniqueKey)
if localData then
localAlreadyHasData = true
else
local starterTab = {
["Followers"] = {},
["Following"] = {}
}
localData = starterTab
end
if targetData then
targetAlreadyHasData = true
else
local starterTab = {
["Followers"] = {},
["Following"] = {}
}
targetData = starterTab
end
local success, err = pcall(function()
if localAlreadyHasData then
FollowDataStore:UpdateAsync(localUniqueKey, function(oldValue)
local newValue = oldValue or {["Followers"] = {}, ["Following"] = {}}
table.insert(newValue["Following"], target, true)
return newValue
end)
else
table.insert(localData["Following"], target, true)
FollowDataStore:SetAsync(localUniqueKey, localData)
end
end)
if success then
else
print("Failed")
end
local success, err = pcall(function()
if targetAlreadyHasData then
FollowDataStore:UpdateAsync(targetUniqueKey, function(oldValue)
local newValue = oldValue or {["Followers"] = {}, ["Following"] = {}}
table.insert(newValue["Followers"], target, true)
return newValue
end)
else
table.insert(targetData["Followers"], target, true)
FollowDataStore:SetAsync(targetUniqueKey, targetData)
end
end)
if success then
else
print("Failed")
end
return true
end
end)
But when the script retrieves the saved table to the DataStore, it only returns the default table. Can someone please help me out? I have looked around the DevForums for other people with the same problem, but none of them have really has the same issue as me.