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

math.random chooses the same number everytime, how do I fix this?

So I'm making a game with a script that teleports players to different maps using math.random. The problem is, the outcome is always the same number, making the same map load over and over again.. How do I fix this? Thanks for stopping by to read this!


local Map1 = game.Lighting.Map1

local Map2 = game.Lighting.Map2

local Map3 = game.Lighting.Map3

local Map4 = game.Lighting.Map4

local Map5 = game.Lighting.Map5


while true do
	
msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Welcome to Zombie Survival! Choosing a map!"

wait(1)

msg:remove()

math.randomseed(tick())
for _ = 1, 5 do
	ChooseMap = math.random(1,5)
	
	


if ChooseMap == 1 then

    map1z = game.Lighting.Map1:clone()
    map1z.Parent = game.Workspace

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(0.5)
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(1)
msg.Text = "Map loaded! Teleporting players..."
msg:remove()
wait(1)

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do

    if plr.Character then

        plr.Character:MoveTo(Vector3.new(14.31, 1.61, -151.635))

      wait(20)

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Game end... Intermission!"
wait(1)
  for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    if plr.Character then
        plr.Character:MoveTo(Vector3.new(1.915, 915.46, -26.68))
msg:remove()

     map1z:Destroy()

wait(10)


    if ChooseMap == 2 then

    map2z = game.Lighting.Map2:clone()
    map2z.Parent = game.Workspace

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(0.5)
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(1)
msg.Text = "Map loaded! Teleporting players..."
msg:remove()
wait(1)

     for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    if plr.Character then
        plr.Character:MoveTo(Vector3.new(23.1, 140.98, -331.42))
 wait(20)

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Game end... Intermission!"
wait(1)
  for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    if plr.Character then
        plr.Character:MoveTo(Vector3.new(1.915, 915.46, -26.68))
msg:remove()

 map2z:Destroy()


wait(10)


  if ChooseMap == 3 then
     map3z = game.Lighting.Map3:clone()
     map3z.Parent = game.Workspace

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(0.5)
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(1)
msg.Text = "Map loaded! Teleporting players..."
msg:remove()
wait(1)

      for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
      if plr.Character then
        plr.Character:MoveTo(Vector3.new(23.1, 220.36, 477.16))
       wait(20)
msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Game end... Intermission!"
wait(1)
  for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    if plr.Character then
        plr.Character:MoveTo(Vector3.new(1.915, 915.46, -26.68))
msg:remove()


       map3z:Destroy()



wait(10)

if ChooseMap == 4 then

    map4z = game.Lighting.Map1:clone()
    map4z.Parent = game.Workspace

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(0.5)
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(1)
msg.Text = "Map loaded! Teleporting players..."
msg:remove()
wait(1)

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do

    if plr.Character then

        plr.Character:MoveTo(Vector3.new(23.1, -200.77, 477.16))

      wait(20)

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Game end... Intermission!"
wait(1)
  for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    if plr.Character then
        plr.Character:MoveTo(Vector3.new(1.915, 915.46, -26.68))
msg:remove()

     map4z:Destroy()

wait(10)

if ChooseMap == 5 then

    map5z = game.Lighting.Map1:clone()
    map5z.Parent = game.Workspace

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(0.5)
msg.Text = "Loading map please wait."
wait(0.5)
msg.Text = "Loading map please wait.."
wait(0.5)
msg.Text = "Loading map please wait..."
wait(1)
msg.Text = "Map loaded! Teleporting players..."
msg:remove()
wait(1)

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do

    if plr.Character then

        plr.Character:MoveTo(Vector3.new(-655.39, 220.36, 477.16))

      wait(20)

msg = Instance.new("Message")
msg.Parent = nil
msg.Parent = game.Workspace
msg.Text = "Game end... Intermission!"
wait(1)
  for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    if plr.Character then
        plr.Character:MoveTo(Vector3.new(1.915, 915.46, -26.68))
msg:remove()

     map5z:Destroy()

  end
 end
 end
  end
 end
 end
  end
  end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end



1 comment

0

I noticed I made two small errors with the Map cloning on 4 and 5. Even though I fixed this, The same thing still happens..

1 answer

✓ Accepted answer

If you get stuck, the first thing you should always do is make your code clean.

This code is a mess.

Tab your code correctly.

You have twenty-six ends in a row -- that's a terrible sign.

You definitely have mistakes of grouping, for example, the for loop on line 54 includes the intermission code -- so you have an intermission per player in the game.

Don't repeat yourself

You have the same chunk of code over and over and over. You should write that as a function instead.

Bad idioms

Don't call math.randomseed more then once -- do it once at the very top of your code. You also probably shouldn't use it directly with tick(). Try math.randomseed(tick()7) or math.randomseed(tick()%1*1e7).

Lowercase methods are deprecated. Use :Remove() not :remove(), :Clone() not :clone().

:Remove() is deprecated in favor of :Destroy(). Use :Destroy() instead.

Use local variables.

Use your variables. You define Map1 but instead refer to game.Lighting.Map1 explicitly.

Use tables. When you have names like Map1, Map2, Map3, ... that means you should be using a list, not variables.

Use the Instance.new to set Parent. And don't re-set properties for no reason. Especially a redundant .Parent = nil.

Don't abbreviate. Silly abbreviations like plr don't gain you anything. Make your code read like prose.

Write comments. If you have a 500 line script with no explanation, good luck revisiting this in a month or two. Explain what things are. Really, if things need explanation, that probably indicates your code can be better, too.

Don't use magic numbers. You have Vector3.new(14.31, 1.61, -151.635) in your code. Where's that? Why not have a standard Home part or similar in the Map?

Cleaned up

As far as I can tell, this basically does the same thing as the script you posted. But in 55 lines instead of 282.

The only change you would have to make is putting a part called Spawn in each model to get the position to place players at.

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

LOBBY = Vector3.new(1.915, 915.46, -26.68)

math.randomseed(tick()7)

while true do
	local msg = Instance.new("Message")
	msg.Parent = nil
	msg.Parent = game.Workspace
	msg.Text = "Welcome to Zombie Survival! Choosing a map!"
	wait(1)
	msg:Destroy()

	local chosenMap = math.random(1,5)
	local map = Maps[chosenMap]:Clone()
	local msg = Instance.new("Message", workspace)

	for _ = 1, 2 do
		for dots = 1, 3 do
			msg.Text = "Loading map please wait" .. string.rep(".", dots)
			wait(0.5)
		end
	end
	wait(0.5)
	msg.Text = "Map loaded! Teleporting players..."
	wait(1)
	msg:Destroy()
	for _, player in paris(game:GetService("Players"):GetPlayers()) do
		if player.Character then
			player.Character:MoveTo(map.Spawn.Position + Vector3.new(0, 5, 0))
		end
	end
	
	local message = Instance.new("Message", workspace)
	message.Text = "Game end... Intermission!"
	wait(1)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character then
			player.Character:MoveTo(LOBBY)
		end
	end
	message:Destroy()
	map:Destroy()
end

Edits

Link is correct. You should use ServerStorage, not Lighting. I have updated to reflect that. This is better organized, and makes your game load faster.

1 comment

2

slaps BlueTaslem around with a fish You encouraged the use of Lighting for storage!

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