I just put that down and it gave me a new error, this is what the error said:
16:49:16.767 - GetService is not a valid member of Players “Players”
Woops. I’m tired today. I meant GetPlayers
.
Whenever more then one person joins the game it gives me an error, but when it is only me it runs fine. For some reason my computers copy and paste is just not working right now.
Could you describe and elaborate the error?
The error is saying attempted to index nil with MoveTo. I am trying to move a random player to an area and it works when one person is in the game but as soon as two or more people join it errors. I put this:
RandomPlayer.Character:MoveTo(Vector3.new(X,Y,Z) but the X Y and Z were actual coordinates.
It is likely that the Character
hasn’t loaded in yet. Try a different approach of populating a table with players who have triggered the CharacterAdded
event and randomize any player from that table.
I added in the player added and character added events and it is still giving me this error:
17:35:58.407 - Workspace.ImportantScripts.MainGameLoop:10: invalid argument #1 to ‘random’ (interval is empty)
Here is my script if this helps:
local RandomPlayer = playerService:GetPlayers()[math.random(#Players)]
game.Players.PlayerAdded:Connect(function(plr)
print("Player Added")
plr.CharacterAdded:Connect(function()
print("Character Added")
if RandomPlayer.Team == game.Teams.Audience then
print("Test Success")
local Judge1 = RandomPlayer
print(Judge1)
Judge1.Team = game.Teams.Judges
Judge1.Character:MoveTo(Vector3.new(107.085, 258.95, 233.305))
local Judge2 = RandomPlayer
print(Judge2)
local Judge3 = RandomPlayer
end
end)
end)
That’s not exactly what I implied on with the other table. Alternatively, try to do an if
statement for the RandomPlayer.Character
. If it doesn’t exist, re-roll until the player is right.
I am sorry, what do you mean by re-roll. Shouldn’t this only be going through players in the game?
Basically by setting the variable anew again:
RandomPlayer = playerService:GetPlayers()[math.random(#Players)]
Ok I put that in the characterAdded event, let me see if this works, thanks!
It still is saying the interval after random is empty even though I put #Players in it.
local playerService = game:GetService("Players")
local Players = playerService:GetPlayers()
local RandomPlayer = Players[math.random(#Players)]
It is still saying the interval after random is empty even though it is not.
I assume you’re running this on the server. You get that error, when your code is prematurely running before the player joins. I would add an if statement
to see if #Players > 0
, before the condition is met.
You are still miscomprehending what I meant, here’s the implicated script.
local Players = game:GetService("Players")
local playersReady = {}
local function pickRandomPlayer()
return playersReady[math.random(#playersReady)]
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Wait() -- this was flawed, as it infinitely added up for each respawn
table.insert(playersReady, player)
end)
Players.PlayerRemoving:Connect(function(player)
local index = table.find(playersReady, player)
if index then
table.remove(playersReady, index)
end
end)
This fully works in my test game:
local playerService = game:GetService("Players")
local players = {}
playerService.PlayerAdded:Connect(function(player)
player.CharacterAdded:Wait()
table.insert(players, player)
-- Check if player was added to table
--[[
if table.find(players, player) then
print("Player Added Successfully!")
else
print("Player Not Added Successfully...")
end
]]
end)
playerService.PlayerRemoving:Connect(function(player)
local PlrToRemove = table.find(players, player)
if PlrToRemove then
table.remove(players, PlrToRemove)
end
end)
local function randomizePlayer()
local RandomNumber = math.random(1, #players)
for i, player in ipairs(game.Players:GetPlayers()) do
if i == RandomNumber then
player.Character.Head.Transparency = 1
end
end
end
-- DON'T CALL THIS IMMEDIATELY!!!
-- vvvv
randomizePlayer()
Here’s a model if you want: PlayerRandomizerScript - Roblox
Hope this helps!
Ok so I needed to access this table from multiple scripts so I used a module script and it works until the random part comes in. I put this:
if #playerReady >= 1 then
print(RandomPlayer)
end
RandomPlayer is equal to pickRandomPlayer(). and for some reason instead of print henberrysodapop it prints nil.
RandomPlayer
does not change by itself. You have to make sure to overwrite it whenever you’re absolutely sure that at least one player exists.
try print(RandomPlayer.Name) instead of just RandomPlayer