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
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)
.
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.
You need to pack every child of maps
into the table, rather than just 'maps'. Then there will be a positve difference!
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
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