I was making a spectate camera. However, I wanted to make the camera prevent from spectating me (LocalPlayer). I tried this but it only works in the "previous" button. This means the camera can "spectate" me when I click the "next" button. Is there a way to fix this?
--Made by CStarLam
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 next = bar.Next -- [!] Don't mess up with the keyword "next" you use in normal coding. It will be the variable for the script.
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 -- Tween it!
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
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) and cam.CameraSubject ~= game.Players.LocalPlayer.Character.Humanoid then
cam.CameraSubject = players[#players].Character.Humanoid end
pcall(function()
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)
end)
next.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) and cam.CameraSubject ~= game.Players.LocalPlayer.Character.Humanoid then
cam.CameraSubject = players[1].Character.Humanoid end
pcall(function()
title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)
end)
Please describe each block of code as well. It can help me learn something new every day! Thank you!
