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

Script not working?? "interval is empty"

Asked by 8 years ago

I asked this before and i thought i got it to work but it says it again... i am trying to teleport 2 random people to a certain spot in the map (665.995,7.1,-115.005) when i hit play it says Workspace.Script:7: bad argument #1 to 'random' (interval is empty)

when i join the game with me and my friend in the same server it says Workplace.Script:7: bad argument #1 to 'random' (interval is empty)

local allPlayers = {}
for a,v in pairs(game.Players:GetPlayers(2)) do
        table.insert(allPlayers,v)
end
local plrAmount = game.Players.NumPlayers
math.randomseed(tick())
local val1 = math.random(plrAmount)
local val2
repeat wait(5)
    val2 = math.random(plrAmount)
until val2 ~=val1

allPlayers[val1].Character.Torso.CFrame = CFrame.new(0,0,0)
allplayers[val2].Character.Torso.CFrame = CFrame.new(665.995,7.1,-115.005)
0
As an additional note, "bad argument #1 to 'random' (interval is empty)" means that your expression is invalid for math.random. Most likely plrAmount is initially 0 (as expected), which is causing the issue. Unclear 1776 — 8y

1 answer

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
8 years ago

math.random takes two parameters, the starting integer and the ending integer. You only have given the ending integer, which is the total amount of players in the game.

You want to search through all the players in the game, so your starting integer would be one. This means it gets the entire table of players from the beginning of the table to the end of the table.

local allPlayers = {}
for a,v in pairs(game.Players:GetPlayers()) do
        table.insert(allPlayers,v)
end
local plrAmount = game.Players.NumPlayers
math.randomseed(tick())
local val1 = math.random(1, plrAmount) --1 is the starting integer while plrAmount is the ending integer
local val2
repeat wait(5)
    val2 = math.random(1, plrAmount) --1 is the starting integer whiel plrAmount is the ending integer
until val2 ~=val1

allPlayers[val1].Character.Torso.CFrame = CFrame.new(0,0,0)
allplayers[val2].Character.Torso.CFrame = CFrame.new(665.995,7.1,-115.005)

Here is information on math.random and what you did wrong.

If I helped you out, be sure to accept my answer!

0
Thank you i think i get it now :D berrythebetta 20 — 8y
Ad

Answer this question