How do I clone an instance from a reference in a table/dictionary?

  1. What do you want to achieve?
    I want to clone an instance from a reference in a table/dictionary to workspace.

  2. What is the issue?
    Code “runs fine” (no errors in console) but nothing actually happens, nothing is duplicated. I can do print(location.name) and that comes out fine, but :Clone() does not work.
    image

  3. What solutions have you tried so far?
    I’ve attempted to do location:Clone().Parent = game.Workspace.DuplicatedMaps as to directly just clone it and not use a variable.

I’ve tried messing around with what works and as I previously said doing location.name works.

I’ve tried looking around the forums for similar topics and all I got was an explanation on about how “When you have instances in a dictionary, those are references. Referencing an instance in a dictionary is as simple as having a key defined as the reference to that instance. You can also access it back by indexing the key to which the instance reference is assigned to. If you’re getting nil back, the reference doesn’t exist.”

Maybe that explanation completely solves my problem and I’m just not understanding it correctly?


-- Severely shortened as to get to the point.
local SERVER_MAPS = {
	["spookySwamp"] = {location = game.Lighting.Maps:WaitForChild("Spooky Swamp"),GameValue = 1,decalID = 10101373358,description = "the spookiest swamp",name = "spookySwamp"};
	["victorianMansion"] = {location = game.Lighting.Maps:WaitForChild("Victorian Mansion"),GameValue = 2,decalID = 10774291,description = "1700s victorian mansion of dooom!!",name = "victorianMansion"};
	["hauntedGraveyard"] = {location = game.Lighting.Maps:WaitForChild("Haunted Graveyard"),GameValue = 3,decalID = 10078796723,description = "that one haunted graveyard ig",name = "hauntedGraveyard"};
	["forbiddenIsle"] = {location = game.Lighting.Maps:WaitForChild("Forbidden Isle"),GameValue = 4,decalID = 10078795186,description = "[Content Deleted] Error 359: Content forbidden.",name = "forbiddenIsle"};
	["forgottenBowlingIsle"] = {location = game.Lighting.Maps:WaitForChild("Forgotten Bowling Isle"),GameValue = 5,decalID = 9893718327,description = "uuuhrh. i.. i forgor description",name = "forgottenBowlingIsle"};
	["desolateForest"] = {location = game.Lighting.Maps:WaitForChild("Desolate Forest"),GameValue = 6,decalID = 9186779263,description = "emerald forest but emo",name = "desolateForest"};
} 

local AmtOfMaps = 0
for i, v in pairs(SERVER_MAPS) do
	AmtOfMaps = AmtOfMaps + 1
end

GAME_START.Event:Connect(function()
	print(" GAME START EVENT FIRED")
	if (PLAYERCOUNT >= PLRCOUNT_MINIMUM) then
		if GAME_STARTED.Value == false then
			local sentText = ""

			if GAME_STARTED.Value == false then
				sentText = "Next round coming shortly..."
			end
			
			CLIENTGUI_UPDATE:FireAllClients(sentText)
			wait(10)
			--
			MAPVOTE_CREATE:Fire()
			for i = 1,20 do 
				wait(T)
				TIMER.Value = TIMER.Value - 1
				CLIENTTIMER_UPDATE:FireAllClients()
			end
			GAME_STARTED.Value = true
			--

			-- // Game
			local function gameRun() 

				if GAME_STARTED.Value == false then
					GAME_STARTED.Value = true
				end

				if VOTE_INSESSION.Value == true then
					VOTE_INSESSION.Value = false
				end
				
				
				CLIENT_GUIWIPE:FireAllClients()
				game.Workspace.DuplicatedMaps:ClearAllChildren()
		
				
				local highest = nil
				local highestName = nil
				for _, v in pairs(VALUES:WaitForChild("SelectedMapValues"):GetChildren()) do 
					print("function: running highestVal table loop ")
					if not highest then
						highest = v.votes.value
						highestName = v.Value
						continue
					end

					if v.votes.value > highest then
						highest = v.votes.value
						highestName = v.Value
					end
					print("function: highestVal table loop ran")
				end

				for i, v in pairs(SERVER_MAPS) do -- something around here breaks and the map is not cloned
					local location = v.location
					if highestName == i then
						local clone = location:Clone()
						clone.Parent = game.Workspace.DuplicatedMaps
					end
				end
				print("function: server maps cloned")
				print(highestName)
				print(highest)
			end


			gameRun()

		end
	end



2 Likes

You can do Dictionary[keyName]:Clone() to clone what you have stored in that key.

I just realize you have another dictionary stored as your value here. You can access the location by pretty much doing the same pattern here:
Dictionary[Dictionary2][key]:clone()

I am confused, I thought using an in pairs loop on SERVER_MAPS (dict1) would allow me to use v as (dict2), and from there I could do v.location(key) and so on.

my thinking process:

local SERVER_MAPS = {
	["spookySwamp"] = {location = game.Lighting.Maps:WaitForChild("Spooky Swamp"),GameValue = 1,decalID = 10101373358,description = "the spookiest swamp",name = "spookySwamp"};
	["victorianMansion"] = {location = game.Lighting.Maps:WaitForChild("Victorian Mansion"),GameValue = 2,decalID = 10774291,description = "1700s victorian mansion of dooom!!",name = "victorianMansion"};
	["hauntedGraveyard"] = {location = game.Lighting.Maps:WaitForChild("Haunted Graveyard"),GameValue = 3,decalID = 10078796723,description = "that one haunted graveyard ig",name = "hauntedGraveyard"};
	["forbiddenIsle"] = {location = game.Lighting.Maps:WaitForChild("Forbidden Isle"),GameValue = 4,decalID = 10078795186,description = "[Content Deleted] Error 359: Content forbidden.",name = "forbiddenIsle"};
	["forgottenBowlingIsle"] = {location = game.Lighting.Maps:WaitForChild("Forgotten Bowling Isle"),GameValue = 5,decalID = 9893718327,description = "uuuhrh. i.. i forgor description",name = "forgottenBowlingIsle"};
	["desolateForest"] = {location = game.Lighting.Maps:WaitForChild("Desolate Forest"),GameValue = 6,decalID = 9186779263,description = "emerald forest but emo",name = "desolateForest"};
} 


local highest = nil
				local highestName = nil
				for _, v in pairs(VALUES:WaitForChild("SelectedMapValues"):GetChildren()) do 
					print("function: running highestVal table loop ")
					if not highest then
						highest = v.votes.value
						highestName = v.Value
						continue
					end

					if v.votes.value > highest then
						highest = v.votes.value
						highestName = v.Value
					end
					print("function: highestVal table loop ran")
				end

				for i, v in pairs(SERVER_MAPS) do -- v is the 2nd dict (ex: ["spookySwamp"])
					if highestName == i then
						local clone = v.location:Clone() -- i could go from v to location (v.location),  location is a instance ( {location = game.Lighting.Maps:WaitForChild("Spooky Swamp")}; )
						clone.Parent = game.Workspace.DuplicatedMaps
					end
				end
				print("function: server maps cloned")
				print(highestName)
				print(highest)

You’re completely correct in thinking that your index is the entry to the dictionary whereas the value is what the index is defining.

v.location is exactly what you would expect an outcome would be from your code - whatever map has been selected.

What exactly doesn’t work?

EDIT: Something you can try in the meantime is doing what @Dragonfable6000 recommended, by using a dictionary pattern in your for loop, it would look something like this:

for i, v in pairs(SERVER_MAPS) do -- v is the 2nd dict (ex: ["spookySwamp"])
					if highestName == i then
						local clone = i["location"]:Clone() -- i could go from v to location (v.location),  location is a instance ( {location = game.Lighting.Maps:WaitForChild("Spooky Swamp")}; )
						clone.Parent = game.Workspace.DuplicatedMaps
					end
				end

Thank you for the advice, I’ll try @Dragonfable6000 's recommendation and see if the problem fixes. The issue is that the instance I’ve referred to does not clone. I suspect it may be an issue outside of the script.

UPDATE: Did not fix the issue.

My best guess is that highestName isn’t being defined properly . . ? We’re not told what what VALUES is in the first pairs loop so verify that works and that highestName is defined and can equal the i variable in the second loop.

If somehow the instance reference is breaking, you could always replace it with the names of the instances and then search for them in the loop using the name.

1 Like

You should debug with something like this:

local SERVER_MAPS = {
	["spookySwamp"] = {location = "Spooky Swamp", GameValue = 1,decalID = 10101373358,description = "the spookiest swamp",name = "spookySwamp"};
	["victorianMansion"] = {location = "Victorian Mansion", GameValue = 2,decalID = 10774291,description = "1700s victorian mansion of dooom!!",name = "victorianMansion"};
	["hauntedGraveyard"] = {location = "Haunted Graveyard", GameValue = 3,decalID = 10078796723,description = "that one haunted graveyard ig",name = "hauntedGraveyard"};
	["forbiddenIsle"] = {location = "Forbidden Isle", GameValue = 4,decalID = 10078795186,description = "[Content Deleted] Error 359: Content forbidden.",name = "forbiddenIsle"};
	["forgottenBowlingIsle"] = {location = "Forgotten Bowling Isle", GameValue = 5,decalID = 9893718327,description = "uuuhrh. i.. i forgor description",name = "forgottenBowlingIsle"};
	["desolateForest"] = {location = "Desolate Forest", GameValue = 6,decalID = 9186779263,description = "emerald forest but emo",name = "desolateForest"};
} 

local Path = game.Lighting:WaitForChild("Maps")

local highestName = "spookySwamp"

for i, v in pairs(SERVER_MAPS) do
	if highestName == i then
		if Path:FindFirstChild(v.location) then
			local clone = Path:FindFirstChild(v.location):Clone()
			clone.Parent = game.Workspace.DuplicatedMaps
		else
			warn(v.location, "folder doesnt exist in Maps folder")
		end
	end
end

Alternatively, you dont even need to perform a loop in the table of maps, if you already have the right name, just FindFirstChild and clone() it:

local Path = game.Lighting:WaitForChild("Maps")
local highestName = "spookySwamp"

if Path:FindFirstChild(highestName) then
	local clone = Path:FindFirstChild(highestName):Clone()
	clone.Parent = game.Workspace.DuplicatedMaps
end
2 Likes

Thank you for the fix and also the alternative method, that’s really efficient way to do it. I didn’t even know warn was a thing and I’ll begin using the proper way to debug instead of using print().

1 Like

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