hi iv'e made a map changer that randomize maps but some time it repeat doing the same map again how do i make it randomize numbers that werent?
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 map8 = game.ServerStorage.Maps.Map8 while true do local random = math.random(1, 8) if random == 1 then map1:Clone().Parent = game.Workspace wait(360) game.Workspace.Map1:Destroy() end if random == 2 then map2:Clone().Parent = game.Workspace wait(360) game.Workspace.Map2:Destroy() end if random == 3 then map3:Clone().Parent = game.Workspace wait(360) game.Workspace.Map3:Destroy() end if random == 4 then map4:Clone().Parent = game.Workspace wait(360) game.Workspace.Map4:Destroy() end if random == 5 then map5:Clone().Parent = game.Workspace wait(360) game.Workspace.Map5:Destroy() end if random == 6 then map6:Clone().Parent = game.Workspace wait(360) game.Workspace.Map6:Destroy() end if random == 7 then map7:Clone().Parent = game.Workspace wait(360) game.Workspace.Map7:Destroy() end if random == 8 then map8:Clone().Parent = game.Workspace wait(360) game.Workspace.Map8:Destroy() end end
thanks :D
I recommend using a table and choosing one of the values in the table.
local maps = {} for i,v in pairs (game.ServerStorage.Maps:GetChildren()) table.insert(maps, #maps +1, v.Name) end for i,v in pairs (maps) do local mapname = maps[math.random(1,#maps)] local map = game.ServerStorage.Maps[mapname] end
That code will take every child of your Maps folder and add it to a table. That will then take a random map from the table and set the value of map to the map.
ex: map.Parent = game.Workspace
Anyways, the thing with math.random
is that it isn't truly random.
RobloxDev states that '...the random number generator is deterministic. It uses the last random number to generate the next random number. When the scripts starts, the “first” random number is always the same.'
math.randomseed
function. It will set a seed for the math.random
function. Let's give it a seed of tick()
. tick()
is just the time in seconds since the Unix epoch (1/1/1970) in local time (your computer).math.randomseed(tick()) -- add this before any calls to math.random! for i = 1, 100 do math.random() -- a little "warm up" for the function end -- Do your code VVVVV