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

randomize numbers that werent help?

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

3 comments

0

btw i dont know how to use code block

0

click the blue lua icon, then paste your self inside the ~~~~

0

ohhh ok thx and can u help me please?

2 answers

✓ Accepted answer

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

1 comment

0

thank you so much!

###Was barely able to read that code. Do paste it in a code block like this.

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.'

###With that in mind, we should use the 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

2 comments

0

You can also use the Random object that recently came out. It has a way better algorithm than math.random anyway.

0

math.random() now uses the same algorithm as Random I think now

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