Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Error with my math.random script?

Asked by
Relatch 550 Moderation Voter
9 years ago

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
0
Umm, well the maps table is only receiving a single option. legobuildermaster 220 — 9y
0
You messed up on line 20. In line 16 you dont have any values inside of the Maps.f attackonkyojin 135 — 9y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
0
Players.Player2.Backpack.LocalScript:18: attempt to index local 'findmaps' (a nil value) Relatch 550 — 9y
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 dragonkeeper467 453 — 9y
0
Try using WaitForChild instead of FindFirstChild on line 17. FindFirstChild could've returned nil and thus throwing that error up. Spongocardo 1991 — 9y
0
@Spongo, not really necessary, but ok i guess... dragonkeeper467 453 — 9y
View all comments (2 more)
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. Spongocardo 1991 — 9y
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 Goulstem 8144 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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
0
Players.Player2.Backpack.LocalScript:21: bad argument #2 to 'random' (interval is empty) Relatch 550 — 9y

Answer this question