Script not navigating through tables correctly

I have this table called temporarycache:
12:44:36.202 ▼ {
[-1] = ▼ {
[1] = :arrow_forward: {…},
[2] = :arrow_forward: {…}
}
} - Server - DStorer:74

[-1] is the player id when testing

The table within my user id holds tables with properties
when i use this script:

print(module.TemporaryCache[Player.UserId])

It prints out this for some reason:
12:52:20.075 ▼ {
[1] = ▼ {
[1] = :arrow_forward: {…},
[2] = :arrow_forward: {…}
}
} - Server - MatchService:61

Shouldn’t it print

{
[1] = :arrow_forward: {…},
[2] = :arrow_forward: {…}
}
this?

You must be putting the tables in an array, so it put them in array slot 1,

Can I see the script again

local function playerAdded(player)
	local success, data = pcall(function ()
		return CardStore:GetAsync(player.UserId)
	end)
	if success then
		if data then
			CardCache[player.UserId] = data
			MatchService.TemporaryCache[player.UserId] = {CardCache[player.UserId]}
		else
			CardCache[player.UserId] = createDefaultDeck()
			MatchService.TemporaryCache[player.UserId] = {CardCache[player.UserId]}
		end
	else
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end

	print(MatchService.TemporaryCache)

	for _, card in pairs(CardCache[player.UserId]) do
--nothing here yet

	end

	CardService.CardCache = CardCache

end

Players.PlayerAdded:Connect(playerAdded)

Yeah on this line you are putting the Cardcache[player.UserId] in a table(without a key so it defaults to an array).

Remove the braces and it’ll work

1 Like

I spent 1 hour looking for where that extra table was coming from :man_facepalming:
lmao

1 Like

Also just so you know it won’t be passing by value it will be passing by reference.

As in tempcache[player.userid] will be pointing to Cardcache[player.userid]

Sorry for the formatting I’m on mobile

1 Like

How can i make it pass a value then? I was planning on having tempcache reset to cardcache’s value
.

You’d have to copy the table.

I’m on mobile right now, but there’s a tutorial page on copying tables on the roblox API site, just search up “copying tables”

1 Like

Ok, I’m back on PC. Lua is not like low level languages where you can specify whether the variable is a pointer or not, so it’s good to know what will be passed by reference and what will be passed by value.

this post explains passing by reference vs value if you don’t understand .


here’s the cloning tables page

1 Like