So, I was making a spectate camera. The camera must NOT spectate myself (the LocalPlayer). The "next" button works fine, but not the "previous" button. Here is a scenario: If there are 4 players and I am Player 1 who is using the spectate camera, the order I spectated after each click on the "previous" button would be like: Player 4 -> Player 3 -> Player 2 -> Player 1 (which is not supposed to be spectated). Is there a way to fix this problem?
There were no errors in the script!!!
Here is the script (LocalScript):
cam = game.Workspace.CurrentCamera
local button = script.Parent:WaitForChild('Button')
local bar = script.Parent:WaitForChild('Bar')
local title = bar.Title
local prev = bar.Prev
local next1 = bar.Next
function get()
for _, v in pairs(game.Players:GetPlayers()) do
if v.Name == title.Text then
return(_)
end
end
end
local debounce = false
button.MouseButton1Click:connect(function()
bar.Visible = true
if debounce == false then
debounce = true
bar:TweenPosition(UDim2.new(0.5, -600, 0.88, -50),"In","Linear",0.25)
pcall(function()
local players = game.Players:GetPlayers()
cam.CameraSubject = players[math.random(1, #players)].Character.Humanoid
if cam.CameraSubject == game.Players.LocalPlayer.Character.Humanoid then
cam.CameraSubject = players[math.random(1, #players)].Character.Humanoid
repeat until cam.CameraSubject ~= game.Players.LocalPlayer.Character.Humanoid
elseif game.Players.NumPlayers == 1 then
cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
else
cam.CameraSubject = players[math.random(1, #players)].Character.Humanoid
end
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)
elseif debounce == true then
debounce = false
pcall(function()
cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
end)
bar:TweenPosition(UDim2.new(-1, -600, 0.88, -50),"Out","Linear",0.25)
end
end)
prev.MouseButton1Click:connect(function()
wait(0.1)
local players = game.Players:GetPlayers()
local num = get()
if not pcall(function() cam.CameraSubject = players[num-1].Character.Humanoid end) then
cam.CameraSubject = players[#players].Character.Humanoid
if cam.CameraSubject == game.Players.LocalPlayer.Character.Humanoid then
cam.CameraSubject = players[#players-1].Character.Humanoid -- Here is the problem!
repeat until cam.CameraSubject ~= game.Players.LocalPlayer.Character.Humanoid
end
end
pcall(function()
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)
end)
next1.MouseButton1Click:connect(function()
wait(0.1)
local players = game.Players:GetPlayers()
local num = get()
if not pcall(function() cam.CameraSubject = players[num+1].Character.Humanoid end) then
cam.CameraSubject = players[1].Character.Humanoid
if cam.CameraSubject == game.Players.LocalPlayer.Character.Humanoid then
cam.CameraSubject = players[2].Character.Humanoid
repeat until cam.CameraSubject ~= game.Players.LocalPlayer.Character.Humanoid
end
end
pcall(function()
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)
end)
If there are any problems with the question. Leave it in the comments below. Thanks!
