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

Random Player Script Won't Work?

Asked by 9 years ago

I'm making a script that chooses a random Player and makes them DJ, But It won't work! Can someone please help?

I changed it up some more and uses someones solution, But I still get the same result! (Ignore the headphones part,)

local Headphones = game.ServerStorage["DJHeadphones"]
local Set = game.Workspace["DJ Set"]
local DJName = Set.CurrentDJ
local Teleport = Set.Teleport
local Players = game.Players:GetPlayers()

DJName.Value = "Choosing New DJ"    

while wait(15) do
        local DJHeadphones = Headphones:Clone()
        local DJ = Players[math.random(1,#Players)]
        DJName.Value = DJ.Name
        DJ:MoveTo(Teleport.Position + 0,3,0)
        DJHeadphones.Parent = DJ.Character
        DJHeadphones.Handle.CFrame = CFrame.new(DJ.Character.Head.Position)
         wait(160)
        DJHeadphones:Destroy()
        DJ:MoveTo(game.Workspace.DanceFloor.Teleport.Position + 0,3,0)
        DJName.Value = "Choosing New DJ"
end

Here's the Output. (Still stayed the same! Other than the dates.)

13:18:14.384 - ServerScriptService.DJChooser:11: bad argument #2 to 'random' (interval is empty) 13:18:14.386 - Script 'ServerScriptService.DJChooser', Line 11

2 answers

Log in to vote
0
Answered by 9 years ago
local Set = game.Workspace["DJ Set"]
local DJName = Set.CurrentDJ
local Teleport = Set.Teleport
local Players = game.Players:GetPlayers()

DJName.Value = "Choosing New DJ"    

while true do
    wait(15)
    if #Players > 0 then
     local DJ = math.random(1,#Players) --You can't find the name of a number
      DJ = Players[DJ] --This is how you get the player.
      DJName.Value = DJ.Name
        DJ.Character:MoveTo(Teleport.Position+Vector3.new(0,3,0))--You must add the numbers when they are in the form of Vector3
       wait(160)
       DJName.Value = "Choosing New DJ"
    end
end

Hope it helps!

0
The problem wasn't with indexing (although it was faulty), but finding a number within the provided range because the first argument was greater than the second (1 > #Players) ImageLabel 1541 — 9y
Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

The error bad argument #line to 'random' (interval is empty) simply implies that 1 is greater than #Players on line 10.

This is most-likely occurring because your code runs before the first player fully loaded into the game. You could solve this by checking that game.Players.NumPlayers, or #Players are both greater than a certain number.

also, DJ is not an actual reference to the Player, but returns the index at which the "random" player is located, in the table.

table[math.random(1, #table)] -- automatically indexes the returns integer, and therefore returns the value at index automatically

Solution

local Set = game.Workspace["DJ Set"]
local DJName = Set.CurrentDJ
local Teleport = Set.Teleport
local Players = game.Players:GetPlayers()

DJName.Value = "Choosing New DJ"    

while wait(15) do
    if #Players > 1 then -- only runs if there are more than one player in the server
        local DJ = Players[math.random(1,#Players)]
        DJName.Value = DJ.Name
        DJ:MoveTo(Teleport.Position + 0,3,0)
         wait(160)
         DJName.Value = "Choosing New DJ"

    else -- runs if #Players is not > 1
        print('Waiting..')
    end
end
0
Actually, If that is the case and there is only 1 player, that player will be chosen all the time, there won't be any error. EzraNehemiah_TF2 3552 — 9y
0
Wait, there is an error, on lines 17-19, if there is only 1 player, the while true do loop will go on forever without a delay, therefore it will lag the game and probably crash it. EzraNehemiah_TF2 3552 — 9y
0
Yeah, but that's not the issue, it isn't crashing because the first run isn't successful. As I stated before, and in my answer, the second argument is less than the first, which is why he got the specific error he posted in his post. ImageLabel 1541 — 9y

Answer this question