Hi can someone help me please. ive created an map changer script for my game. it works perfect until it comes to the last map and changes to the first map again but it clones only the model with nothing inside how to fix it? thanks for helping
local Map1 = game.ServerStorage.Maps.Map1 local Map2 = game.ServerStorage.Maps.Map2 local Map3 = game.ServerStorage.Maps.Map3 local Map4 = game.ServerStorage.Maps.Map4 local Map5 = game.ServerStorage.Maps.Map5 local Map6 = game.ServerStorage.Maps.Map6 local Map7 = game.ServerStorage.Maps.Map7 local Map1Clone = Map1:Clone() local Map2Clone = Map2:Clone() local Map3Clone = Map3:Clone() local Map4Clone = Map4:Clone() local Map5Clone = Map5:Clone() local Map6Clone = Map6:Clone() local Map7Clone = Map7:Clone() while true do Map1Clone.Parent = game.Workspace Map1Clone.Name = "Map1Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to Crossroads") wait(1) game.Workspace.Map1Clone:remove() game.Workspace.Message:remove() Map2Clone.Parent = game.Workspace Map2Clone.Name = "Map2Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to IDK3") wait(1) game.Workspace.Map2Clone:remove() game.Workspace.Message:remove() Map3Clone.Parent = game.Workspace Map3Clone.Name = "Map3Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to IDK4") wait(1) game.Workspace.Message:remove() game.Workspace.Map3Clone:remove() Map4Clone.Parent = game.Workspace Map4Clone.Name = "Map4Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to IDK5") wait(1) game.Workspace.Message:remove() game.Workspace.Map4Clone:remove() Map5Clone.Parent = game.Workspace Map5Clone.Name = "Map5Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to IDK6") wait(1) game.Workspace.Message:remove() game.Workspace.Map5Clone:remove() Map6Clone.Parent = game.Workspace Map6Clone.Name = "Map6Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to IDK7") wait(1) game.Workspace.Message:remove() game.Workspace.Map6Clone:remove() Map7Clone.Parent = game.Workspace Map7Clone.Name = "Map7Clone" wait(5) MSG = Instance.new("Message") MSG.Parent = game.Workspace MSG.Text = ("Changing to The Forest") wait(1) game.Workspace.Message:remove() game.Workspace.Map7Clone:remove() wait(0) end
Alright, well something you may want to work on is working with tables Tables make things so much more clean.
Alright, so I went thing things and rewrote your code, just a bit more clean.
I did my best to explain it, if you have any questions at all, Please feel free to ask! ``` local Maps = game:GetService('ServerStorage').Maps -- This is getting all the maps in a table
local Message = Instance.new('Message') -- creates the message
local loopTime = 5 -- How long it takes to go through the loop lol
local mapMessages = -- what the message says when each map is selected
{ -- When adding a map OR chaning the name of the map, you need to change each name on here as well
Map1 = 'Randomly Selected Map1',
Map2 = 'Randomly Selected Map2',
Map3 = 'Randomly Selected Map3',
Map4 = 'Randomly Selected Map4',
Map5 = 'Randomly Selected Map5',
Map6 = 'Randomly Selected Map6',
Map7 = 'Randomly Selected Map7',
Map8 = 'Randomly Selected Map8',
}
local mapClones = {} -- Getting a clone of each map and putting them in a table
for i,v in pairs (Maps:GetChildren()) do -- getting the children of 'Maps'
mapClones[i] = v -- Assigning the map in the table
end -- ending it o.O
while true do -- creating the loop
local map = mapClones[math.random(#mapClones)]:Clone() -- clones a random map out of the selection
Message.Text = mapMessages[map.Name] -- Makes the message whatever the 'mapMessage' is for selected map
map.Parent = workspace -- parents the Map to workspace so that it can be seen
Message.Parent = workspace -- parents the message to workspace so that it can be seen
wait(loopTime-1) -- Waits one second less than looptime so that we can give the game 1 second to reset thigns
map:Destroy() -- Destroys the map
Message.Parent = nil -- Sets the parent of Message to nil (Makes the message go away temporarily lol)
wait(1) -- waits one second to restart the loop
end ```