local player = game.Players.LocalPlayer
cam = game.Workspace.CurrentCamera
local bar = script.Parent:WaitForChild('Bar')
local title = bar.Player
local prev = bar.Previous
prev.MouseButton1Click:connect(function()
wait(0.1)
for i, players in pairs(game.Players:GetPlayers()) do
if players ~= player then
cam.CameraSubject = players.Character.Humanoid
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
title.TextColor3 = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).TeamColor.Color
end
end
end)
I’m trying to get all the players in a server, except the local player (for a spectate gui) so I want the button to basically cycle through all the players in a server, but never come to the local player, only the other players. Even so it still just cycles through every single player. What am I missing?
prev.MouseButton1Click:connect(function()
wait(0.1)
for i, player in pairs(game.Players:GetPlayers()) do
if player ~= game.Players.LocalPlayer then -- this was the problem, the second value in the for loop is the value in the table, it's not the table itself (also the player wont be named "Player1")
cam.CameraSubject = players[i + 1].Character.Humanoid -- no idea what you're doing here?
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
title.TextColor3 = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).TeamColor.Color
end
end
end)
since you’re cycling through every single player at once, the current way you wrote the script cycles through every player in one frame, so it automatically goes to the last player in Players:GetChildren()
yes because the player value isn’t a table, you’d want to do player.Character.Humanoid instead
even then you’d still only be focused on the last player and would only be able to spectate them since you’re cycling through every player in one frame
If that’s what you’re trying to achieve, you’ll want to create a list of players to rotate through before hand. Currently you’re iterating through every player, and then landing on the last one, which is not how you want to do it.
This code should allow you to actually rotate through the players
local playersList = game.Players:GetPlayers() -- Gets all the players in the server
local currentPlayer = 1
-- This for loop removes the local player from the list so you don't spectate yourself
for i, player in pairs(playersList) do
if player == game.Players.LocalPlayer then
table.remove(playersList, i)
break
end
end
prev.MouseButton1Click:connect(function()
-- Now I edited your previous function to allow for rotating through the players list
wait(0.1)
local spec = playersList[currentPlayer]
cam.CameraSubject = spec.Character.Humanoid
title.Text = spec.Name
title.TextColor3 = spec.TeamColor.Color
currentPlayer = currentPlayer + 1
if currentPlayer > #playersList then
currentPlayer = 1
end
end)
The program keeps track of the current player spectating, and a table of players to spectate. To rotate through the players, the currentPlayer variable is incremented every time the button is pushed. This code works, but the playersList table is static, and in a game you would want it to be dynamic(able to change as players come and go), but it’s a good stepping stone to work from.