Table being overwritten in datastore/how to add an existing table to a dictionary

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)
1 Like

This doesn’t return anything unless route is not found in the table

3 Likes

But wouldn’t it set it I ran two different routes? say first run is “easy-medium” and second run is “easy-verv”, shouldn’t easy-verv be saved

1 Like

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

2 Likes

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.

1 Like

Try running:

print(plrData[plrId])
1 Like

it prints nil :thinking:
charssssssssssss

1 Like

There’s the problem! Looking at everything again, it should be plrData[route][plrId]

3 Likes

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 :face_holding_back_tears:

1 Like

Don’t forget this:

deMainData[route] = {} -- this part wasn't in the code sample you sent me
deMainData[route][plrId] = plrData[route][plrId]

I don’t believe I understand (maybe the above part solves your issue?)

3 Likes

Sorry I meant on this line it errors

routes[route][plrId] = plrData[route][plrId] --inside where if the data is nil

I can’t see why that line would error; maybe you could change it to this?

local routes = {}
routes[route] = {
   [plrId] = plrData[route][plrId]
}
1 Like

Yep! seems to work. Thank you so much, saved me many hours of pain

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.