Issue with map changing command (moves model to workspace and destroys other model in workspace)

Hey everyone. I’m trying to make a command that the admins of the script can change the map. Though, only map 1 and 2 work, map 3 and 4 don’t. I’ve tried this and the other inside my full code as of finding the part and deleting it:

if not game.Workspace.Map1 then
   game.Workspace:FindFirstChild("Map2"):Destroy()
end

Here’s my full script, sorry if it’s a bit messy I’m a starter in programming. :grin:

--// ADMINS
local admins = {"jees1"};

--// MAPS
local map1 = game.ServerStorage.Map1
local map2 = game.ServerStorage.Map2
local map3 = game.ServerStorage.Map3
local map4 = game.ServerStorage.Map4

--// ADMIN CHECK
function checkAdmin(speaker)
    local isAdmin = false;
    for a,b in pairs (admins) do
		        if b == speaker.Name then
			print("User is an admin")
            isAdmin = true;
            break;
        end
    end
    return isAdmin
end

--// CODE
function onChatted(msg, speaker)
    if checkAdmin(speaker) then
        msg = string.lower(msg)
		    if msg == "!changemap map1" then
			map1:Clone().Parent = game.Workspace
			game.Workspace:FindFirstChild("Map2"):Destroy()
			if not game.Workspace:FindFirstChild("Map2") then
				game.Workspace:FindFirstChild("Map3"):Destroy()
				if not game.Workspace:FindFirstChild("Map3") then
					game.Workspace:FindFirstChild("Map4"):Destroy()
					if not game.Workspace:FindFirstChild("Map4") then
						print("Changed to map 1")
					end
				end
			end
		elseif msg == "!changemap map2" then
			map2:Clone().Parent = game.Workspace
			game.Workspace:FindFirstChild("Map1"):Destroy()
			if not game.Workspace:FindFirstChild("Map1") then
				game.Workspace:FindFirstChild("Map3"):Destroy()
				if not game.Workspace:FindFirstChild("Map3") then
					game.Workspace:FindFirstChild("Map4"):Destroy()
					print("Changed to map 2")
				end
            end
        end
	elseif msg == "!changemap map3" then
		map3:Clone().Parent = game.Workspace
			game.Workspace:FindFirstChild("Map1"):Destroy()
			if not game.Workspace:FindFirstChild("Map1") then
				game.Workspace:FindFirstChild("Map2"):Destroy()
				if not game.Workspace:FindFirstChild("Map2") then
					game.Workspace:FindFirstChild("Map4"):Destroy()
					if not game.Workspace:FindFirstChild("Map4") then
					print("Changed to map 3")
				end
			end
		end
	elseif msg == "!changemap map4" then
		map4:Clone().Parent = game.Workspace
			game.Workspace:FindFirstChild("Map1"):Destroy()
			if not game.Workspace:FindFirstChild("Map1") then
				game.Workspace:FindFirstChild("Map2"):Destroy()
				if not game.Workspace:FindFirstChild("Map2") then
					game.Workspace:FindFirstChild("Map3"):Destroy()
					if not game.Workspace:FindFirstChild("Map3") then
					print("Changed to map 4")
				end
			end
		end
	end
end
game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)onChatted(msg, player) end)
end)

Any help is appreciated, thank you!

Well, let’s simplify this a little bit.

local Admins = {"jees1"}

local Maps = {
game.ServerStorage.Map1;
game.ServerStorage.Map2;
game.ServerStorage.Map3;
game.ServerStorage.Map4;
}

local Folder = Instance.new("Folder")
Folder.Name = "MapsHolder"
Folder.Parent = workspace

function CheckAdmin(speaker)
	return table.find(Admins,speaker.Name) 
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg,rec)
		if msg then
			if CheckAdmin(player) then
				if string.sub(msg,1,11) == "!changemap " then
					local newmsg = string.sub(msg,12)
					if tonumber(newmsg) then
						Folder:ClearAllChildren()
						Maps[tonumber(newmsg)]:Clone().Parent = Folder
					end
				end
			end
		end
	end)
end)

Wasn’t able to test this so I’m not sure if it will work as intended, however let me know if it doesn’t. For clarification, use the commands as such:

!changemap 1
!changemap 2
etc.

Let’s start from the top:

The “CheckAdmin” function is bulky and can be shortened down to 1 line using table.find, that’s more of an apperance thing than anything else though. table.find() in this scenario uses (table, value).

There is a very clunky use of “FindFirstChild” as I’m sure you agree and that is no doubt contributing to errors so I’ve shortened it down using a simple method. By parenting all of the maps to a single folder you can use :ClearAllChildren() to remove all the maps, eliminating the need for all of the sanity checks.

You separated the “OnChatted” and player.Chatted in a very odd way so I’ve just grouped them together to eliminate that, however that’s also just an appearance issue.

Wow, thank you so much. I’ll check if this works soon, though just to make sure, if I want to add more maps do I just have to add it to the local variable of maps on a new line formatted the same?

Yes, it’d just be as simple as adding a new value to the table, for example:

local Maps = {
game.ServerStorage.Map1;
game.ServerStorage.Map2;
game.ServerStorage.Map3;
game.ServerStorage.Map4;
game.ServerStorage.Map5;
game.ServerStorage.Map6;
game.ServerStorage.Map7;
}

And you wouldn’t have to make any other changes.

That’s great. I just tested your code and it works! Thank you so much for your help and even responding to me. I really appreciate it. Have a great day/night.