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
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!
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
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