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

The Map Randomly chooses but The Both Maps Load, Why?

So When The Countdown ends, The Both "Desert Map" and "RedMap" Load, I'm Scripting it so only one of them can load, But they load both on top of each other, Please Help!

local s = script.Stat
local Players = game:GetService("Players")
local PlayerCount = 0

game.Players.PlayerAdded:Connect(function()
	PlayerCount = PlayerCount + 1
end)

game.Players.PlayerRemoving:Connect(function()
	PlayerCount = PlayerCount - 1
end)

while PlayerCount < 1 do
	s.Value = "We Need More Players"
	repeat wait(1) until PlayerCount >= 1
end

t = 0
while true do
	t = 8
	repeat
		t = t-1
		s.Value = "Map Loading in "..t
		wait(1)
	until t == 0
------------------Game 
local Maps = {'DesertMap','RedMap'}
local randomMap = math.random(1, #Maps)

for i,v in pairs(Maps) do
	local storage = game:GetService("ReplicatedStorage")
	local map = storage.Maps:WaitForChild(v):clone()
	map.Parent = workspace.mapsInGame
	
end

	t = 10
	repeat
		t = t-1
		s.Value = t.." seconds left"
		wait(1)
	until t == 0
	workspace.mapsInGame:ClearAllChildren()
	end

function teleAll(x, y, z)
local posl = Vector3.new(game.Workspace.SpawnLocation)
	for m, plr in pairs(game:GetService("Players"):GetPlayers()) do
		if plr.Character then
			plr.Character:MoveTo(posl)
end

	end
end
	s.Value = "Game Ended!"

wait(10)

4 comments

0

this is a duplicated thread

0

i already answered in the previous 1

0

I deleted it, Because the format was wrong, pls answer

0

format was the exact same. this is the 3rd time ive seen this.

2 answers

✓ Accepted answer

Hello, my name is TheOnlySmarts, and I'd be glad to help you with your problem.


On line 30, you're using for i,v in pairs(maps) do. But you aren't actually choosing a map out of the two.

So in this situation we'll use a math.random.

math.random's are exceptionally useful to determine a random value, object or number. We can also use it for maps, case opening, etc.

A math.random simply gets a random input out of the other values. Here is a quick example.

local numbers = {1, 2, 3, 4, 5}

local numberChosen = math.random(1, #numbers) -- Choosing a random number

Doing this simply gathers a random number out of the numbers table, now I haven't used tables in a while, so that script may be wrong. But that is a little base of how it'd be used.

However with your script, we'll choose a random map, not a number.

for i,v in pairs(Maps) do
	local mapChosen = math.random(1, #Maps)

	local storage = game:GetService("ReplicatedStorage")
	local map = storage.Maps:WaitForChild(mapChosen):clone()
	map.Parent = workspace.mapsInGame
end

And wuh-la!!

You have yourself a functioning Map Randomiser, well. Hopefully this helped, have a great rest of your day or night.


Tip : If this answer has helped you or somebody else you know, click 'Accept Answer' below to show love to the author, letting him/her know it has helped. It'll also give you both positive reputation, which is good for the community, as it brings a better understanding to a user!

On the line 30 you typed "for i,v in ipair(maps) do" so you're get all children and now they're called v. On the line 32-33 you're cloning them (v) to the workspace , you're not choosing a random one.

7 comments

0

So how I choose a random one

0

this is clearly not your script.

0

How it's not my script?

0

So How Do I Choose a random map?

0

Use math.random. So do after the for i,v in pairs(maps) bla bla bla write local mapChosen = (1, #maps) . then add ur v thing to the random map.

0

Here I'll make an answer.

0

there, answer made, hopefully it helps, okay bubai :D

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