Okay, so this isn't really a scripting question persey. Maybe more of a logic question? Either way, I need help. I'm making a spectating system, and the crux here is that I don't want the player to be able to spectate themselves (because I'm picky). So, instead of just being able to add or subtract from the iteration of my :GetPlayers() table, I have to work with some frustrating logic to avoid selecting my own player. Here's the code:
local function SpectatePlayer(Iteration,Action)
local SelectedPlayer = Players:GetPlayers()[Iteration]
if SelectedPlayer then
if SelectedPlayer ~= Player then
if SelectedPlayer.Character and SelectedPlayer.Character:FindFirstChild("Humanoid") then
SpectateFrame.PlayerName.Text = SelectedPlayer.Name
PlayerCamera.CameraSubject = SelectedPlayer.Character.Humanoid
end
else
if Action == "Back" then
SpectatorIteration -= 1
SelectedPlayer = Players:GetPlayers()[SpectatorIteration]
if SelectedPlayer.Character and SelectedPlayer.Character:FindFirstChild("Humanoid") then
SpectateFrame.PlayerName.Text = SelectedPlayer.Name
PlayerCamera.CameraSubject = SelectedPlayer.Character.Humanoid
end
elseif Action == "Forward" then
SpectatorIteration += 1
SelectedPlayer = Players:GetPlayers()[SpectatorIteration]
if SelectedPlayer.Character and SelectedPlayer.Character:FindFirstChild("Humanoid") then
SpectateFrame.PlayerName.Text = SelectedPlayer.Name
PlayerCamera.CameraSubject = SelectedPlayer.Character.Humanoid
end
end
end
end
end
SpectateFrame.Back.Activated:Connect(function()
if SpectatorIteration >= 2 then
SpectatorIteration -= 1
SpectatePlayer(SpectatorIteration,"Back")
elseif SpectatorIteration == 1 then
SpectatorIteration = #Players:GetPlayers()
SpectatePlayer(SpectatorIteration,"Back")
end
end)
SpectateFrame.Forward.Activated:Connect(function()
if SpectatorIteration <= #Players:GetPlayers() then
SpectatorIteration += 1
SpectatePlayer(SpectatorIteration,"Forward")
elseif SpectatorIteration == #Players:GetPlayers() then
SpectatorIteration = 1
SpectatePlayer(SpectatorIteration,"Forward")
end
end)
##Issue #1
I attempted to make it so when you reached the last player in the server and clicked the forward button again, it would reset to the beginning of the list. For example, if there were 5 players in a server, and I am currently spectating the fifth player, if I click "forward" again, it will go back to the first player. Likewise, for the "back" button, if I am on the first player, and I click the "back" button, it will go to the last player in the list. All this must happen while respecting the fact that I do not want the player to be able to spectate themselves (I tried to solve that in the section of the function where it checks the Action string).
##Issue #2
It seems that I have to click the back/forward buttons twice before they actually do anything. Maybe because I used >= and <=? But I need those operators to ensure that the value does not go 0 or below, or higher than the amount of players in the server.
Again, I apologize for what seems like a rather silly question. My brain can't seem to wrap around it though! Any help would be greatly appreciated.
##Note: The SpectatorIteration variable is handled when activating my spectating GUI. It essentially just sets itself to the first player in the :GetPlayers() table that is NOT the local player.

2 comments
Well, I currently fixed your Forward button problem. When, I'll able to fix the Back button, I'll post the fixes.
@BestCreativeBoy will probably solve this, but this quite the dilemma. Imma join in to give my brain an exercise.