Need help with Spectating bug

Long story short I was testing my spectate script but when I click the previous buttion it works but When I click the next buttion it errors

When I tried to debug using prints it printed out this

yes1 - Client - Spectating Script:65
12:47:35.028 ▼ {
[1] = Player2,
[2] = Player1,
[3] = Player3
} - Client - Spectating Script:66

Script:

local Player = game.Players.LocalPlayer
local NextButtion = Player.PlayerGui.SpectatingUI.Frame:WaitForChild("Next")
local ProviousButtion = Player.PlayerGui.SpectatingUI.Frame:WaitForChild("Previous")
local GameInProgress = game.ReplicatedStorage:WaitForChild("GameInProgress_Repli")
local Camera = workspace.CurrentCamera
local Humanoid = Player.Character:WaitForChild("Humanoid")
local CloseButton = Player.PlayerGui.SpectatingUI:WaitForChild("ImageButton")
local PlayerSpecatingText = Player.PlayerGui.SpectatingUI.Frame:WaitForChild("TextLabel")
local OpenButtion = Player.PlayerGui.MenuButtons:WaitForChild("SpectatingButton")

local AvailableToSpectate = {}


OpenButtion.MouseButton1Click:Connect(function()
	PlayerSpecatingText.Text = Player.Name
end)


NextButtion.MouseButton1Click:Connect(function()
	if GameInProgress.Value == true then
		
		
		Camera.CameraSubject = AvailableToSpectate[#AvailableToSpectate+1].Character.Humanoid
		PlayerSpecatingText.Text = AvailableToSpectate[#AvailableToSpectate+1].Name
	end
end)

CloseButton.MouseButton1Click:Connect(function()
	Camera.CameraSubject = Player.Character.Humanoid
	print("yes4")
end)




ProviousButtion.MouseButton1Click:Connect(function()
	if GameInProgress.Value == true then

		Camera.CameraSubject = AvailableToSpectate[#AvailableToSpectate-1].Character.Humanoid
		PlayerSpecatingText.Text = AvailableToSpectate[#AvailableToSpectate-1].Name
	end	
end)

Humanoid.Died:Connect(function()
	if table.find(AvailableToSpectate, Player.Name) and not Player:FindFirstChild("Student") and not Player:FindFirstChild("Baldi") then
		for Index, V in pairs(AvailableToSpectate) do
			if V == Player.Name then
				table.remove(AvailableToSpectate, Index)
				print("yes3")
			end
		end
	end
end)



GameInProgress:GetPropertyChangedSignal("Value"):Connect(function()
	if GameInProgress.Value == true then
			for i, v in pairs(game.Players:GetPlayers()) do
				if v:FindFirstChild("Student") then
					table.insert(AvailableToSpectate,v)
				elseif v:FindFirstChild("Baldi") then
					table.insert(AvailableToSpectate,v)	
			end	
			print("yes1")
			print(AvailableToSpectate)
		end
	end	
end)

Hello. Please go into detail about your issue.

What happens when you click on the next button?

It gives me a error “attempt to index nil with character”

And which line is this error at?

Checking right now
(30 words requirement)

Line 23
and I also want to tell you that the script is a local script

That’s because your table index is going out of range. You need to check to make sure it doesn’t.

--in this example, current is the index of the current player you are spectating
current += 1
if current > table.maxn(availableToSpectate) then
    current = 1
end

local newPlayer = availableToSpectate[current]
--etc, etc

--by the way capital letter beginning variable names are typically used for classes
--so that might get a bit confusing for some people

Confusing for me
(30 words requirement)

That was just a side note. It’s to do with variable naming conventions.

Typically, the practice is:

  • Use a lowercase beginning letter name for a variable (e.g. currentPlayer)
  • Use an uppercase beginning letter name for a class (classes are used in OOP)
  • Use a full caps variable name for a constant
  • Avoid single-letter names (iteration is an exception to this)

How can I implement this into my script?

Your current script will always try and reach one above the table’s index. Here’s a brief structure:

  • Hold a variable that counts for the index of the player you are spectating
  • Update the current players available whenever needed
  • Whenever going to the next player, increment this variable by 1
  • Whenever going to the previous player, subtract 1 from this variable
  • Make sure the check the index is not out of range by checking against the length of the table
  • Set the new text and camera subject for the newly spectated player

Okay Um I’m not really that advanced of a scripter so can you help me?

I’m sorry bro but this will take a substantial amount of time which I just don’t have at the moment… what I would recommend though is searching some tutorials on YouTube, I’m sure there are some great ones there and if u have the skill level you might be able to further adapt it to your game.

Sorry bro good luck :smiley:

Okay thanks!
(30 words requirement)

1 Like

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