Players.Puma_incredible.PlayerGui.SpectatingUI.Frame.Spectating Script:13: attempt to index nil with 'Character'

I’m trying to make a spectating script and while I was testing It I ran into a error (title) but No matter what I do I can’t fix it please help

The AvailableToSpectate Table is a table full of player objects and in the NextButton Function it uses the .Character thing to get the player object but then it errors

Code:

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 AvailableToSpectate = {}

NextButtion.MouseButton1Click:Connect(function()
	
	local CharacterofChoosen = ChoosePlayerToSpecate(AvailableToSpectate)

	Camera.CFrame = Vector3.new{CFrame = CharacterofChoosen.Character.HumanoidRootPart.CFrame}

end)




function ChoosePlayerToSpecate(AvailableToSpectate)
	if GameInProgress.Value == true then
		local RandomObj = Random.new()
		
		local ChoosenPlayerToSpectate = AvailableToSpectate[RandomObj:NextInteger(1,#AvailableToSpectate)]
		
		
		return ChoosenPlayerToSpectate	
	end
end

GameInProgress:GetPropertyChangedSignal("Value"):Connect(function()
	if GameInProgress.Value == true then
		while wait(0.5) do

			AvailableToSpectate = {}	

			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	
			end

			ChoosePlayerToSpecate(AvailableToSpectate)
			
			if GameInProgress.Value == false then
				break
			end
			
			print("yes")
		end
	end	
end)

If you have a solution please tell!

it looks like the table resets every time you loop

Yeah it does
I made it like that so when a Student or Baldi dies the table removes them
(30 words requirement)

yeah idk im lost, its also kinda hard to figure this script out when you cant see the menus and how it works

Okay So first the GameInprogress BoolValue turns true when the round starts and goes false,

In the while wait loop it checks for tags that is inserted into players if a player is a Student they get a Student tag if their baldi then they get a Baldi tag,

The availableToSpectate table is something that allows the script to loop through all the players to add them into the table

This function

Chooses a random player to spectate to test


function ChoosePlayerToSpecate(AvailableToSpectate)
	if GameInProgress.Value == true then
		local RandomObj = Random.new()
		
		local ChoosenPlayerToSpectate = AvailableToSpectate[RandomObj:NextInteger(1,#AvailableToSpectate)]
		
		
		return ChoosenPlayerToSpectate	
	end
end

and then returns the choosen player

Are you still confused or not?

Do the players have a tag named “Student” or “Baldi” when the while wait(0.5) do loop begins?

Yes They do
(30 words requirement)

Try printing AvailableToSpectate, does it print all of the players correctly?

I don’t really need to print the table of AvailableToSpectate because from testing I already know it works

Wait I fixed the problem by doing this

Camera.CameraSubject = CharacterofChoosen.Character.Humanoid

But Now I need a way to remove players form the AvailableToSpactate table without clearing it

And I have to edit it so everytime you click the spectate button It chooses another player to spectate

I don’t get this exactly. Don’t you have to clear the table to remove the players, or am I misunderstanding something?

I want to only remove a player from the table when they die But I don’t know how to do that I know about table.remove But How to actually tell if a player dies and if they were in the table before they died

Here’s an example of how it should work:

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")

	local AvailableToSpectate = {}

	Humanoid.Died:Connect(function()
		if table.find(AvailableToSpectate, Player.Name) then
			for Index, V in pairs(AvailableToSpectate) do
				if tostring(V) == Player.Name then
					table.remove(AvailableToSpectate, Index)
				end
			end
		end
	end)
end)

Testing it right now
(30 words requirement)

It works! I’ma take it form where thank you!
(30 words requirement)

1 Like

You’re welcome! Good luck on your project :wink:

Thank you so much!
(30 word requirement)

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