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)
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
function ChoosePlayerToSpecate(AvailableToSpectate)
if GameInProgress.Value == true then
local RandomObj = Random.new()
local ChoosenPlayerToSpectate = AvailableToSpectate[RandomObj:NextInteger(1,#AvailableToSpectate)]
return ChoosenPlayerToSpectate
end
end
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
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)