For some information, in my speedrun game, I’m trying to have a datastore key hold all information about a set route as a secondary key to retrieve data faster. But every time I try to add an existing table it overwrites the previous table or sometimes does not update at all. I’ve tried many ways of referencing the route to create the table but none of them work for me. Here’s the code, please help, am I being stupid?:
local function saveToMainData(plr, plrData, route, plrId)
print(plrData) -- {["easy-wall"] = etc
print(route) --easy-verv first call, then easy-wall
print(plrId) --plr's id
print(typeof(route)) --string
print(typeof(plrId)) --string
local setSuccess, errorMessage = pcall(function()
leaderboardsStore:UpdateAsync("MainData", function(pastMainData)
if pastMainData == nil then
print("data nil")
local d = HttpService:JSONEncode(plrData)
print(d)
return d
end
print(pastMainData) --{"easy-verv":{etc} only saving a single run despite doing two runs
local deMainData = HttpService:JSONDecode(pastMainData)
print(deMainData) ---["easy-verv"]= etc
if not deMainData[route] then
deMainData[route] = plrData[plrId]
print(deMainData) --no change
return HttpService:JSONEncode(deMainData)
end
end)
end)
end
Here’s how the data is transferred
local function saveRun(plr, Time, s1, s2)
local plrData = {
[s1.. "-" ..s2] = {
[plrId] = {
Time,
0
}
}
}
local route = s1.."-"..s2
local plrId = tostring(plr.UserId)
task.spawn(saveToMainData, plr, plrData, route, plrId)
I’m going to try to put this into words as best as I can: basically, what’s happening is that your code is not appending new information to the pre-existing table; it creates the table if it doesn’t exist, but doesn’t do anything with it if it does exist:
-- Creates the table if it doesn't exist, however, this does nothing to the data if the table does exist
-- Therefore any new information is not written down and thus not stored
if not deMainData[route] then
deMainData[route] = plrData[plrId]
print(deMainData) --no change
return HttpService:JSONEncode(deMainData)
end
You should be storing the route information like something like:
deMainData[route] = {} -- Create the table
deMainData[route][plrId] = plrData[plrId] -- Store the player's information in a table
Then update it like so:
if not deMainData[route] then
-- ...
else
deMainData[route][plrId] = plrData[plrId]
return HttpService:JSONEncode(deMainData)
end
Atleast this is assuming that I’m understanding your intentions correctly
Forgive me if I’m misinterpreting or if I implemented it wrong but it doesn’t seem to be updating the table still.
local function saveToMainData(plr, plrData, route, plrId)
print(plrData) -- {["easy-wall"] = etc
print(route) --easy-verv first call, then easy-wall
print(plrId) --plr's id
print(typeof(route)) --string
print(typeof(plrId)) --string
local setSuccess, errorMessage = pcall(function()
leaderboardsStore:UpdateAsync("MainData", function(pastMainData)
if pastMainData == nil then
local routes = {}
routes[route] = {}
local d = HttpService:JSONEncode(routes)
print(d)
return d
end
print(pastMainData) --{"easy-wall":[]}
local deMainData = HttpService:JSONDecode(pastMainData)
print(deMainData) ---["easy-wall"] = {}
if not deMainData[route] then
deMainData[route] = plrData[plrId]
print(deMainData) --["easy-wall"] = {} printed despite me running a different route
return HttpService:JSONEncode(deMainData)
else
deMainData[route][plrId] = plrData[plrId]
return HttpService:JSONEncode(deMainData)
end
end)
end)
end
If I initially explained wrong, what I want to achieve is saving the route/plrData when saveToMainData is called always. Also the game plays by running from different points, allowing the player to choose a dynamic route from any point in the map for more information. Sorry if I interpreted wrong, thanks.
Is this how I should be able to get the playerid as a table in the route?
if pastMainData == nil then
local routes = {}
routes[route] = {}
routes[route][plrId] = plrData[route][plrId]
local d = HttpService:JSONEncode(routes)
print(d)
return d
end
local deMainData = HttpService:JSONDecode(pastMainData)
if not deMainData[route] then
deMainData[route][plrId] = plrData[route][plrId]
print(deMainData)
return HttpService:JSONEncode(deMainData)
else
deMainData[route][plrId] = plrData[route][plrId]
print(deMainData)
return HttpService:JSONEncode(deMainData)
end
end)
end)
end
ServerScriptService.Datastore:91: attempt to index nil with '861475515' --my id (error)
Do I have to create the playerId after the route is created? It almost worked instantly but it put the data in the wrong place so I had to change it