Old spectator script bug

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)```

add return v in the get() function inside if v.Name == inttext.Text then end as just return is breaking the code

so, still doing the same thing, i can’t spectate other users than the first one.

i rewrite the entire script (kinda)
hope this works

local Camera = workspace.CurrentCamera
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 currentspec
local players = {}

players = game:GetService("Teams").Survivors:GetPlayers()

game:GetService("Teams").Survivors.PlayerAdded:Connect(function(new)
    table.insert(players,new)
end)

game:GetService("Teams").Survivors.PlayerRemoved:Connect(function(plr)
    table.remove(players,plr)
end)

button.MouseButton1Click:Connect(function()
	if debounce == false then 
        debounce = true
		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)
        currentspec = players[1]
        Camera.CameraSubject = players[currentspec].Character.Humanoid
        inttext.Text = players[currentspec].Name
        inttext.Parent.Dropshadow.Text = inttext.Text
	elseif debounce == true then debounce = false
		Camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		maininterface.Visible = false
	end
end)


prev.MouseButton1Click:Connect(function()
	task.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)

    if players[currentspec+1] then
        currentspec = currentspec-1
    else
        currentspec = players[#players]
    end

    Camera.CameraSubject = players[currentspec].Character.Humanoid

    inttext.Text = players[currentspec].Name
	inttext.Parent.Dropshadow.Text = inttext.Text
end)

nex.MouseButton1Click:Connect(function()
	task.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)

    if players[currentspec+1] then
        currentspec = currentspec+1
    else
        currentspec = 1
    end

    Camera.CameraSubject = players[currentspec].Character.Humanoid

    inttext.Text = players[currentspec].Name
	inttext.Parent.Dropshadow.Text = inttext.Text
end)

I tried your rewrite but the script just broke.

The error:

my bad, fixed the script

local Camera = workspace.CurrentCamera
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 currentspec
local players = {}

players = game:GetService("Teams").Survivors:GetPlayers()

game:GetService("Teams").Survivors.PlayerAdded:Connect(function(new)
    table.insert(players,new)
end)

game:GetService("Teams").Survivors.PlayerRemoved:Connect(function(plr)
    table.remove(players,plr)
end)

button.MouseButton1Click:Connect(function()
	if debounce == false then 
        debounce = true
		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)
        currentspec = 1
        Camera.CameraSubject = players[currentspec].Character.Humanoid
        inttext.Text = players[currentspec].Name
        inttext.Parent.Dropshadow.Text = inttext.Text
	elseif debounce == true then debounce = false
		Camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
		maininterface.Visible = false
	end
end)


prev.MouseButton1Click:Connect(function()
	task.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)

    if players[currentspec+1] then
        currentspec = currentspec-1
    else
        currentspec = #players
    end

    Camera.CameraSubject = players[currentspec].Character.Humanoid

    inttext.Text = players[currentspec].Name
	inttext.Parent.Dropshadow.Text = inttext.Text
end)

nex.MouseButton1Click:Connect(function()
	task.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)

    if players[currentspec+1] then
        currentspec = currentspec+1
    else
        currentspec = 1
    end

    Camera.CameraSubject = players[currentspec].Character.Humanoid

    inttext.Text = players[currentspec].Name
	inttext.Parent.Dropshadow.Text = inttext.Text
end)

Your fix kinda worked, i just needed to add/modify some things.
I modified only the way that the script remove the players from the table.

Old:

game:GetService("Teams").Survivors.PlayerRemoved:Connect(function(plr)
    table.remove(players,plr)
end)

New:

game:GetService("Teams").Survivors.PlayerRemoved:Connect(function(plr)
	local index = table.find(players, plr) -- Find the index of the player in the table
	if index then
		table.remove(players, index) -- Remove the player using the index
	end
end)

But anyways, thank you!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.