I recently ported a script from a very old game that’s similar to the one I’m currently working on. However, I’m not very familiar with how the script works, and I’m having trouble getting it to function properly. It seems that there’s a simple error somewhere in the code that’s preventing it from working as intended.
The issue is that the script only works for one player at a time. Once that player leaves the team or dies, the script does not stops functioning altogether, but instead it works again, gets the other player, but i can’t swap to the next/previous. I’ve been trying to figure out what’s causing this issue, but I haven’t been able to pinpoint it yet.
If anyone has any suggestions or insights into how to solve this problem, I would greatly appreciate it. Thank you!
local Camera = workspace.CurrentCamera
local Players = game.Players
local button = script.Parent.Parent.Parent.ButtonStock.Spectate.SpectateCopy
local maininterface = script.Parent.Parent.Parent.MainInterfaces.Spectate
local prev = maininterface.PreviousButton
local nex = maininterface.NextButton
local inttext = maininterface.PlayerName
local debounce = false
local playersTeam = game:GetService("Teams").Survivors:GetPlayers()
function get() -- pega todos os players
for _,v in pairs(game:GetService("Teams").Survivors:GetPlayers()) do
if v.Name == inttext.Text then
return
end
end
end
local num = get()
local players = game:GetService("Teams").Survivors:GetPlayers()
pcall(function()
repeat
Camera.CameraSubject = players[num+1].Character.Humanoid
until Camera.CameraSubject ~= nil
end)
button.MouseButton1Click:Connect(function() -- ao clicar no botão, abre a interface de espectar
if debounce == false then debounce = true
local players = game:GetService("Teams").Survivors:GetPlayers()
maininterface.Visible = true
maininterface.SpectateOn:Play()
maininterface.Position = UDim2.new(0.382, 0,0.701, 0)
maininterface:TweenPosition(UDim2.new(0.382, 0,0.651, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .2, true)
pcall(function()
inttext.Text = game.Players:GetPlayerFromCharacter(Camera.CameraSubject.Parent).Name wait()
inttext.Parent.Dropshadow.Text = inttext.Text
end)
elseif debounce == true then debounce = false
pcall(function() Camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid end)
maininterface.Visible = false
end
end)
prev.MouseButton1Click:connect(function() -- botão de voltar para o player que estava antes de seguir
wait(.1)
script.Sound:Play()
maininterface.Position = UDim2.new(0.382, 0,0.701, 0)
maininterface:TweenPosition(UDim2.new(0.382, 0,0.651, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .2, true)
local players = game:GetService("Teams").Survivors:GetPlayers()
local num = get()
if not pcall(function()
Camera.CameraSubject = players[num-1].Character.Humanoid
end)
then
Camera.CameraSubject = players[#players].Character.Humanoid
wait()
inttext.Text = game.Players:GetPlayerFromCharacter(Camera.CameraSubject.Parent).Name
inttext.Parent.Dropshadow.Text = inttext.Text
end
pcall(function()
end)
end)
nex.MouseButton1Click:connect(function() -- botão de seguir para o player
wait(.1)
script.Sound:Play()
maininterface.Position = UDim2.new(0.382, 0,0.701, 0)
maininterface:TweenPosition(UDim2.new(0.382, 0,0.651, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Back, .2, true)
local players = game:GetService("Teams").Survivors:GetPlayers()
local num = get()
if not pcall(function()
Camera.CameraSubject = players[num+1].Character.Humanoid
end)
then
Camera.CameraSubject = players[#players].Character.Humanoid
wait()
inttext.Text = game.Players:GetPlayerFromCharacter(Camera.CameraSubject.Parent).Name
inttext.Parent.Dropshadow.Text = inttext.Text
end
pcall(function()
end)
end)
pcall(function()
inttext.Text = game.Players:GetPlayerFromCharacter(Camera.CameraSubject.Parent).Name
end)```