Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
130363 67
7 years ago

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

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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)
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? 130363 67 — 7y
0
You can initially set the current_map variable to your map that is in the workspace :) Goulstem 8144 — 7y
Ad

Answer this question