Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make the script remove the old map and then spawn the new map?

So basically, My script, you have to type the command "run/..." DotDotDot meaning the map name. Which I done. But I want the script to also remove the old map and replace it with the new map.

local current_map;

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(chatmsg)
		if chatmsg:sub(1,4) == "run/" then
			local map = game.ServerStorage:FindFirstChild(chatmsg:sub(5))
			local backup = map:clone()
			if map then
				if current_map then
					current_map.Parent = game.ServerStorage
				end
				map = backup:clone()
				map.Parent = workspace.Objects
				current_map = map
			end
		end
	end)
end)

1 answer

✓ Accepted answer

Are you sure this doesn't work? It worked perfectly fine when I replicated the situation in studio.

You do have a few logical mishaps, but nothing that would break the code.

  • You're defining backup prior to checking if the map exists. This will throw an error if you type the wrong name in. But again, wouldn't break the code.

  • You're parenting current_map back into ServerStorage when you cloned it in the first place. This will duplicate the object.

Go try it again. It does work. Even without these edits

local current_map;

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(chatmsg)
		if chatmsg:sub(1,4) == "run/" then
			local map = game.ServerStorage:FindFirstChild(chatmsg:sub(5))
			if map then
				if current_map then
					current_map:Destroy();
				end
				map = map:Clone()
				map.Parent = workspace;
				current_map = map
			end
		end
	end)
end)

2 comments

0

Thanks for your help Goulstem :-) but I have a question how do i make it remove the map that is already there when the game starts then when you run the command it will remove that map that is already there and then spawn the new map?

0

You can initially set the current_map variable to your map that is in the workspace :)

This discussion is preserved as it was. New answers, comments, and votes are closed.