Value between two modules is connected and i dont want that

currently I’m trying to make a checkpoint system using modules to “save” a value

the module value i want to save

local example = {}
example.group = {}

-- example.group gets changed alot throughout the game
return example

the problem is when i try to set a value inside another module to example.group, the value gets sort of “connected” and this would happend

-- this is another module
-- lets say i want example.group current value, i would do this.
local savedvalued = example.group

-- and then after awhile, example.group gets updated, but i run a 
-- function to set example.group to savedvalue. 
example.group = savedvalue 

this doesnt work though, savedvalue would have example.group current value and not the one i wanted saved beforehand.

how would i make it so that savedvalue stays the same without being changed if example.group ever gets updated?

local savedvalued = table.clone(example.group)
1 Like

it was that simple, thank you so much!

1 Like

for nested tables, use a deepcopy function

local function DeepCopy(original)
	local clone = table.clone(original) 

	for key, value in original do
		if type(value) == "table" then
			clone[key] = DeepCopy(value)
		end
	end

	return clone
end

(this one was made by roblox)

2 Likes

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