Is a spectate function automatic?

I made two scripts for a spectate function I was creating, and there was something REALLY weird. The function that I thought would actually switch camera focus over to a certain Character apparently did not do anything. So I commented out the line, and it actually functions fine. Here are scripts:

local pchosen = 0
player = game.Players:GetChildren()
button = script.Parent
cam = workspace.CurrentCamera

button.MouseButton1Click:Connect(function()
     pchosen = pchosen + 1
     if pchosen > #player then
          pchosen = 1
     end
     script.Parent.Parent.PlayerName.Text = tostring(player[pchosen]) --this outputs the name of the player currently being spectated to a TextLabel, which is this script's parent.
     --cam.CameraSubject = player[pchosen].Character.HumanoidRootPart  <<<<<< THIS IS THE COMMENTED OUT LINE THAT I THOUGHT WOULD ACTUALLY DO THE SPECTATING!
end)

This is the script that should initiate the spectating:

local button = script.Parent
local toggle = false
local cam = workspace.CurrentCamera

button.MouseButton1Click:Connect(function()
     --print("Click registered at " .. tostring(script) .. " childed under " .. tostring(script.Parent) .. " within " .. tostring(script.Parent.Parent) .. " under game.StarterGui.")
     cam = workspace.CurrentCamera
     toggle = not toggle
     if toggle == true then
          --cam.CameraType = Enum.CameraType.Track  <<< Another thing that I thought would help initiate spectating...
          game.StarterGui.Spectate.SpectateMainTray.Active = true
     else
          cam.CameraType = Enum.CameraType.Custom
          cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
          game.StarterGui.Spectate.SpectateMainTray.Active = false
     end
end)

So, my question is, does Roblox Studio automatically detect when you are trying to make a spectate function or is this a really weird glitch?

1 Like

Lol I doubt Roblox automatically detects if you’re trying to make a spectate function. Based on the code you provided, I can’t really tell what’s causing it to actually work. You must have some other code that you didn’t post here.

1 Like

Change your commented out line to be Humanoid, not HumanoidRootPart and then uncomment it. See if that works.

1 Like

The problem here is when you set the camera subject.
You are trying to index a string in a array of players, which will return nil. Change the line too:

cam.CameraSubject = game:GetService("Players")[player[pchosen]].Character.Humanoid

Also, set the subject to the humanoid. HumanoidRootPart would work, but if the player jumped into the void it might be weird.

1 Like

Nothing is ever really automatic. This is an issue regarding your implementation and should be revised.

Cameras on characters are focused on their Humanoids, not their HumanoidRootParts, for one. The next thing is that when you’re creating a spectator system, you should get the camera set up right away. That and deny spectating if there are no players that meet the criteria for being able to be spectated.

No spectate:

  • CameraSubject is Humanoid of LocalPlayer’s Character
  • CameraType is Custom

Spectate:

  • CameraSubject is another Humanoid
  • CameraType can be whatever you wish

There’s something else interfering, I can almost tell. In any case; try breezing over your code again. I can’t see any immediate issues with it.


OP uses GetChildren from the Player service (the canonical method is GetPlayers though), therefore the upvalue players references an array of players. OP is using the upvalue pchoice as a numerical index for player; this is perfectly fine.

The tostring is only used to update the TextLabel. It’s not being used to enter the player array.

1 Like

If you just want like a free orbit camera, just set the CameraSubject nil, if you are looking to do a spectate where you cycle through players have all players in a table and have the script go through it and pick one each time you press a arrow or next. The Camera subject will need to be set to the Head.

1 Like

That’s already what OP is doing. I wouldn’t recommend setting the CameraSubject to the Head though. Cameras for avatars are usually done by their Humanoids, not a BasePart descendant of the character. The camera would look weird and bouncy if the head was updated in any manner as well, such as via an animation or whatever. Humanoids keep the camera still and focused on the character.

1 Like

Actually, no. This is the code.

Really weird…I just commented out ALL references to workspace.CurrentCamera and the spectate function still works (with a few bugs, though)! I tested with my alt account. How is it possible that the cam is still switching players even though there is nothing actually switching the camera?

EDIT: I deleted some of the comments and the camera no longer switches. Is it possible that my copy of Roblox Studio has an interpreter error?

Actually, no I didn’t. That was literally it.

No, if you downloaded studio directly from roblox then you should not have any weird issues that others wouldn’t have. This doesn’t sound like an issue with studio anyways.

What lines of code did you comment out which caused it to stop working? It would help if you provided us with more information. Otherwise we can’t help you. The code in the OP does not show anything that would be causing the camera to switch between players.

In the second script I deleted the if/else statement. I forgot exactly what I did.

Yes, I did download Studio directly from ROBLOX. Very strange issue that I encountered. I was using a “ehhhh…” computer yesterday, so I am not sure that using a computer like that could corrupt the place file.

NOTE: “Ehhh…” means slow.

Roblox doesn’t have anything similar to a spectate system. The only thing which will change the camera to look at another player is what you code. If you’ve disabled your two scripts, but things are still happening when you click the button, you probably have another script hiding somewhere which is changing it.

Problem I noticed:
player is defined at the beginning of the script, but never updated. If another play joins after the local player joins, they will be unable to spectate that player. Move the player variable into the MouseClick function.

If you still can’t figure out why the code is functioning differently than it should, upload the place file and we should be able to figure it out.

My guess would be something to do with how you are testing the script.

Maybe you didnt publish/save or something

Well, I kinda fixed it by deleting all spectate guis and starting afresh, and it seems to work. I can try to upload the place file.

It all seemed to work for me, except for the fact that the camera would not return to the player after closing the spectating GUI. I fixed it by adding
cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
to the end of the else statement inside of the SpecOnOff script.

If you still think something is off about the spectating, try and clearly describe what it is, because it all seems to work to me.

I rewrote the while thing. The original setup had all of those weird glitches.

Do you think something spontaneous happened when I was programming it last night?

It was probably a typo or a mistake somewhere. Is it working now? Or is there still a problem?