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

Random Map changer problem?

I'm trying to make a random map chooser and it picks one map but then after it says "changing mapz" the message goes away and the map stays there and I want it to keep changing randomly.

local ran = math.random(1,5)
local msg = Instance.new("Message")
msg.Parent = game.Workspace

while true do
if ran == 1 then
	game.Lighting.field1:clone().Parent = game.Workspace
	game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: City"
	wait(30)
	msg.Text = "Changing mapz"
	wait(2)
	msg:remove()
	wait(2)
	game.Workspace.field1:remove()
elseif ran == 2 then
	game.Lighting.field2:clone().Parent = game.Workspace
	game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: Original"
	wait(30)
	msg.Text = "Changing mapz"
	wait(2)
	msg:remove()
	wait(2)
	game.Workspace.field2:remove()
elseif ran == 3 then
	game.Lighting.field3:clone().Parent = game.Workspace
	game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: ClockWork Village"
	wait(30)
	msg.Text = "Changing mapz"
	wait(2)
	msg:remove()
	wait(2)
	game.Workspace.field3:remove()
elseif ran == 4 then
	game.Lighting.field4:clone().Parent = game.Workspace
	game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: Mario Bros."
	wait(30)
	msg.Text = "Changing mapz"
	wait(2)
	msg:remove()
	wait(2)
	game.Workspace.field4:remove()
elseif ran == 5 then
	game.Lighting.field5:clone().Parent = game.Workspace
	game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: Mount Doom"
	wait(30)
	msg.Text = "Changing mapz"
	wait(2)
	msg:remove()
	wait(2)
	game.Workspace.field5:remove()
end
end

3 answers

✓ Accepted answer

The variable ranis a random value set at the beginning of the script, meaning it NEVER changes from the first random result. Try putting this within the end of the while loop:

ran = math.random(1,5)

So do I do

local ran = math.random(1,5)
local msg = Instance.new("Message")
msg.Parent = game.Workspace

while true do
if ran == 1 then
    game.Lighting.field1:clone().Parent = game.Workspace
    game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: City"
    wait(30)
    msg.Text = "Changing mapz"
    wait(2)
    msg:remove()
    wait(2)
    game.Workspace.field1:remove()
elseif ran == 2 then
    game.Lighting.field2:clone().Parent = game.Workspace
    game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: Original"
    wait(30)
    msg.Text = "Changing mapz"
    wait(2)
    msg:remove()
    wait(2)
    game.Workspace.field2:remove()
elseif ran == 3 then
    game.Lighting.field3:clone().Parent = game.Workspace
    game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: ClockWork Village"
    wait(30)
    msg.Text = "Changing mapz"
    wait(2)
    msg:remove()
    wait(2)
    game.Workspace.field3:remove()
elseif ran == 4 then
    game.Lighting.field4:clone().Parent = game.Workspace
    game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: Mario Bros."
    wait(30)
    msg.Text = "Changing mapz"
    wait(2)
    msg:remove()
    wait(2)
    game.Workspace.field4:remove()
elseif ran == 5 then
    game.Lighting.field5:clone().Parent = game.Workspace
    game.StarterGui.CurrentMap.TextLabel.Text = "Current Map: Mount Doom"
    wait(30)
    msg.Text = "Changing mapz"
    wait(2)
    msg:remove()
    wait(2)
    game.Workspace.field5:remove()
end
ran = math.random(1,5)
end

CamKiller10 meant the ran variable should be placed right...

local msg = Instance.new("Message")
msg.Parent = game.Workspace

while true do
local ran = math.random(1,5)				-- Here.
if ran == 1 then

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