Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My attempt at a spectate GUI isn't spectating?

I honestly have no idea what I tried doing at the bottom, it's my first time ever using camera manipulation which explains why it isn't working. The part where it changes the name works, but where it's supposed to follow their Humanoid isn't working whatsoever. I'm not getting an error either. Any help would be appreciated and I would like to understand how to make it move between the players.

Error: Players.Player2.PlayerGui.SpectateGui.LocalScript:62: attempt to index field '?' (a nil value).

cam = workspace.CurrentCamera

local gui = script.Parent

local players = game:GetService("Players")

local bar = gui:WaitForChild("Bar")
local name = bar:WaitForChild("Name")
local nxt = bar:WaitForChild("Next")
local prv = bar:WaitForChild("Previous")

local button = gui:WaitForChild("Button")

local open = false
local openinterval = 3

possibleplayers = {}
playercount = 0

local function playerstable()
    for _, player in pairs(players:GetPlayers()) do
        if player.Name == name.Text then
            return
        else
            table.insert(possibleplayers, player)
        end
    end
end

button.MouseButton1Click:Connect(function()
    if not open then
        bar:TweenPosition(UDim2.new(0.5, 0, 0.85, 0), "Out", "Quad", 1.2)
        wait(openinterval)
        open = true
    elseif open then
        bar:TweenPosition(UDim2.new(0.5, 0, 1.05, 0), "In", "Back", 1.2)
        wait(openinterval)
        open = false
    end
end)

nxt.MouseButton1Click:Connect(function()
    local allplayers = players:GetPlayers()
    local playernumber = playerstable()
	
	if cam.CameraSubject == possibleplayers[#possibleplayers].Character:FindFirstChild("Humanoid") then
		cam.CameraSubject = possibleplayers[1].Character:FindFirstChild("Humanoid")
	else
    	cam.CameraSubject = possibleplayers[playercount + 1].Character:FindFirstChild("Humanoid")
	end

    name.Text = players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)

nxt.MouseButton1Click:Connect(function()
    local allplayers = players:GetPlayers()
    local playernumber = playerstable()
	
	if cam.CameraSubject == possibleplayers[#possibleplayers].Character:FindFirstChild("Humanoid") then
		cam.CameraSubject = possibleplayers[1].Character:FindFirstChild("Humanoid")
	else
    	cam.CameraSubject = possibleplayers[playercount - 1].Character:FindFirstChild("Humanoid")
	end

    name.Text = players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)


2 answers

I believe what you will have to do is change the CameraType that is being used. I would suggest going to the documentation to see what would work best for you.

Your code would change to look like this (not going to repost everything) around 40 or so.

cam.CameraType = Enum.CameraType.Watch
cam.CameraSubject = players[players - 1].Character:FindFirstChild("Humanoid")

To return to just viewing the character with normal controls, set the CameraType to Custom.

cam.CameraType = Enum.CameraType.Custom

The original script has changed I believe. There is now an error posted. Please look to the other answers for help.

4 comments

0

To make it move between characters, I would suggest using the Interpolate function. http://wiki.roblox.com/index.php?title=API:Class/Camera/Interpolate

0

They give no example on the Interpolate wiki...

0

No, all you need is to change camera subject.

0

I was assuming by move he meant that he wanted to do it differently (smooth gliding between each position) than switching the camera subject. My answer is what I am guessing works for simply switching like hiimgoodpack says.

The problem is that you cant add a table to 1, (line 47). This is how i fixed it.

--NEW VARIABLE
Playercount = 0

cam = workspace.CurrentCamera

cam = workspace.CurrentCamera

local gui = script.Parent

local players = game:GetService("Players")

local bar = gui:WaitForChild("Bar")
local name = bar:WaitForChild("Name")
local nxt = bar:WaitForChild("Next")
local prv = bar:WaitForChild("Previous")

local button = gui:WaitForChild("Button")

local open = false
local openinterval = 3

possibleplayers = {}

local function playerstable()
    for _, player in pairs(players:GetPlayers()) do
        if player.Name == name.Text then
            return
        else
            table.insert(possibleplayers, player)
        end
    end
end

button.MouseButton1Click:Connect(function()
    if not open then
        bar:TweenPosition(UDim2.new(0.5, 0, 0.85, 0), "Out", "Quad", 1.2)
        wait(openinterval)
        open = true
    elseif open then
        bar:TweenPosition(UDim2.new(0.5, 0, 1.05, 0), "In", "Back", 1.2)
        cam.CameraType = Enum.CameraType.Custom
        wait(openinterval)
        open = false
    end
end)

nxt.MouseButton1Click:Connect(function()
    local allplayers = players:GetPlayers()
    local playernumber = playerstable()

    cam.CameraType = Enum.CameraType.Watch
-- FIXED CODE
if cam.CameraSubject = possibleplayers[#possibleplayers].Character.Head then
 cam.CameraSubject = possibleplayers[1].Character.Head
else
    cam.CameraSubject = possibleplayers[PlayerCount + 1].Character.Head
end

    name.Text = players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)

prv.MouseButton1Click:Connect(function()
    local allplayers = players:GetPlayers()
    local playernumber = playerstable()

    cam.CameraType = Enum.CameraType.Watch
    if cam.CameraSubject = possibleplayers[#possibleplayers].Character.Head then
 cam.CameraSubject = possibleplayers[1].Character.Head
else
    cam.CameraSubject = possibleplayers[PlayerCount + 1].Character.Head
end
    name.Text = players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
end)

5 comments

0

to elaborate, you did cam.Subject = possibleplayers[possibleplayers +1]. Possibleplayers +1 isn't possible because possible players is a table

0

Your suggestion didn't work, and isn't returning an error.

0

Here, i edited my code. Try it now. I make the camera subject the head

0

I updated the code attached to the question using what you suggested but changed the position of some things (like playercount, moving it lower in the variable section) and I'm now getting an error. The error is: Players.Player2.PlayerGui.SpectateGui.LocalScript:62: attempt to index field '?' (a nil value).

0

I will need to see your code, as i cant see the exact line of code the problem is going on at. Update your question with the new code.

This discussion is preserved as it was. New answers, comments, and votes are closed.