Strange dictionary behavior. Can someone explain?

local PlayerData = {
		["player1"] ={

		}
	}
	local AllQuests = {
		{
			["Quests"] = 0
		}
	}
	
	local randomQuest = math.random(#AllQuests)
	local choosenQuest = AllQuests[1]--[randomQuest]
	local ID = 1
	PlayerData["player1"][1] = choosenQuest
	PlayerData["player1"][1]["ID"] = 1
	print(AllQuests)

This is what AllQuests prints as
image

My goal was to clone AllQuests’s format into PlayerData so that I can use it as a template to setup other quests, but it seems to connect the two. So whenever I edit AllQuests that that’s in PlayerData, its now editing it in the original AllQuests as well.

That’s called a pointer (reference).

That’s just how tables work.

If you were to increase the value below in 10 different scripts simultaneously 10*10. value would become 100. Because all the scripts have access to the same table through a reference.

local t = {
   value = 10
}
return t

What you want to do is table.clone(AllQuests[1]) to create a new table, where the elements of the previous table are given to the new table.

table.clone is a shallowcopy, it would work in this case, but if you had another table within allQuests[1], you’d need a deepcopy.

I recommend you to not learn luau as your first programming language. Because it won’t teach you things like objects (storage in RAM), compilers and interpreters until it’s too late.

Do you know what JIT stands for in the LuaJIT? JIT stands for just-in-time compiler, LuaJIT is a compiled and interpreted programming language.

There’s plenty of cool stuff outside Roblox.

I would highly recommend to learn from the Lua manual: Lua 5.4 Reference Manual - contents
Even better: www.learncpp.com

2 Likes

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