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

Error with my math.random script?

This script contains 2 functions, parts that run my game. I get an error Players.Player2.Backpack.LocalScript:20: bad argument #2 to 'random' (interval is empty) when it's time to choose the map. Any ideas why?

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:WaitForChild("Messages")
local messageline = messagegui:WaitForChild("MessageLine")

repeat wait() until player and messageline and messagegui

function CheckPlayers()
    if game.Players.NumPlayers >= 2 then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Wait for 2 or more players."
    end
end

function ChooseMap()
    local maps = {}
    local findmaps = game.ReplicatedStorage:WaitForChild("Maps")
    for i,v in pairs(findmaps:GetChildren()) do
        table.insert(maps, v)
    end
    local chosenmap = maps[math.random(1, #maps)]
    chosenmap.Parent = workspace
end

while wait(1) do
	repeat CheckPlayers()
	until game.Players.NumPlayers >= 2
    wait(3)
    messageline.Text = "Choosing map."
    ChooseMap()
	wait(2)
end

2 comments

0

Umm, well the maps table is only receiving a single option.

0

You messed up on line 20. In line 16 you dont have any values inside of the Maps.f

2 answers

Definition of Your Error

You error means that there was no positive difference between your arguments with the math.random function. What I mean by that is if you have math.random(1,1) then there's no positive difference.. same goes for math.random(5,2).

The math.random function has to have a positive difference between the arguments.. like so; math.random(1,4).


Why You Got This Error

You're using math.random(1,#maps) on line 20.. but there is only 1 index in your table. So you're essentially saying math.random(1,1) - which has no positive difference.


How To Fix

You need to pack every child of maps into the table, rather than just 'maps'. Then there will be a positve difference!


Code

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:WaitForChild("Messages")
local messageline = messagegui:WaitForChild("MessageLine")

repeat wait() until player and messageline and messagegui

function CheckPlayers()
	if game.Players.NumPlayers >= 2 then
		messageline.Text = "Game about to start."
	else
		messageline.Text = "Wait for 2 or more players."
	end
end

function ChooseMap()
	local maps = {}
	local findmaps = game.ServerStorage:FindFirstChild("Maps")
	--Insert all the children
	for i,v in pairs(findmaps:GetChildren()) do
		table.insert(maps, v)
	end
	--Now you can index it randomely
	 local chosenmap = maps[math.random(1, #maps)]
	chosenmap.Parent = workspace
end

while wait(1) do
	CheckPlayers()
	wait(5)
	messageline.Text = "Choosing map."
	ChooseMap()
end

6 comments

0

Players.Player2.Backpack.LocalScript:18: attempt to index local 'findmaps' (a nil value)

0

What exactly is Maps in ServerStorage? Please specify! If Maps is a folder then goul is right in getting the children of the folder

0

Try using WaitForChild instead of FindFirstChild on line 17. FindFirstChild could've returned nil and thus throwing that error up.

0

@Spongo, not really necessary, but ok i guess...

0

@Dragon It is necessary, FindFirstChild isn't always reliable. Also, I would suggest getting rid of line 5 altogether, it's not necessary as you're already yielding the script with the WaitForChild function.

0

When the script is trying to find 'Maps' in serverstorage, it's not there. That's not my fault. You need to either use WaitForChild or make sure it's actually there

While Goul's script should work, you're trying to access ServerStorage from a LocalScript. You need to move your Maps model/folder to a place that's readable by a LocalScript, such as ReplicatedStorage.

This is becuase ROBLOX can't access the ServerStorage and ServerScriptService from a LocalScript as LocalScripts run on the client and can't read server items.

So, once you've moved your Maps model/folder to ReplicatedStorage, just use Goul's script but change ServerStorage to ReplicatedStorage on line 17:

local player = game.Players.LocalPlayer
local messagegui = player.PlayerGui:WaitForChild("Messages")
local messageline = messagegui:WaitForChild("MessageLine")

repeat wait() until player and messageline and messagegui

function CheckPlayers()
    if game.Players.NumPlayers >= 2 then
        messageline.Text = "Game about to start."
    else
        messageline.Text = "Wait for 2 or more players."
    end
end

function ChooseMap()
    local maps = {}
    local findmaps = game.ReplicatedStorage:WaitForChild("Maps") --Still using WaitForChild, in case it doesn't appear automatically.
    --Insert all the children
    for i,v in pairs(findmaps:GetChildren()) do
        table.insert(maps, v)
    end
    --Now you can index it randomely
     local chosenmap = maps[math.random(1, #maps)]
    chosenmap.Parent = workspace
end

while wait(1) do
    CheckPlayers()
    wait(5)
    messageline.Text = "Choosing map."
    ChooseMap()
end

1 comment

0

Players.Player2.Backpack.LocalScript:21: bad argument #2 to 'random' (interval is empty)

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