SpectatorUI support

I made a SpectatorUI today and it works great! but there is a problem…
When I click on the UI it spectates another player but when I close the spectate button the camera is just stuck on the player I was spectating.
Is there a way to make it so that while I’m spectating another player and I close the spectate button the camera switches back to my character?

Here’s the script:

local toggle = script.Parent.Toggle
local frame = script.Parent.SpecFrame
local previous = frame.Prev
local next = frame.Next
local status = frame.StatusRound.Status
local camera = game.Workspace.CurrentCamera
local num = 1

status.Text = game.Players.LocalPlayer.Name

toggle.MouseButton1Click:Connect(function()
frame.Visible = not frame.Visible

end)

previous.MouseButton1Click:Connect(function()
local players = game.Players:GetChildren()
local max = #players
num = num - 1
if num < 1 then
num = max
end
local player = players[num]
camera.CameraSubject = player.Character.Humanoid
status.Text = player.Name
end)

next.MouseButton1Click:Connect(function()
local players = game.Players:GetChildren()
local max = #players
num = num + 1
if num > max then
num = 1
end
local player = players[num]
camera.CameraSubject = player.Character.Humanoid
status.Text = player.Name
end)

frame.Changed:Connect(function()
if not frame.Visible then
local Players = game:GetService(“Players”)

	local localPlayer = Players.LocalPlayer

	local function resetCameraSubject()
		if workspace.CurrentCamera and localPlayer.Character then
			local humanoid = localPlayer.Character:FindFirstChildOfClass("Humanoid")
			if humanoid then
				workspace.CurrentCamera.CameraSubject = humanoid
			end
		end
	end

end

end)

1 Like

Here is some code I got from here

local Cam = workspace.CurrentCamera

local Char = game.Players.LocalPlayer.Character

Cam.CameraSubject = Char.Humanoid

Cam.CameraType = "Custom"

Cam.CFrame = Char.Head.CFrame
1 Like

Hi! thanks for replying.
Where do I insert the code? Do I replace it with this code:

local localPlayer = Players.LocalPlayer

local function resetCameraSubject()
	if workspace.CurrentCamera and localPlayer.Character then
		local humanoid = localPlayer.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			workspace.CurrentCamera.CameraSubject = humanoid
		end
	end
end

end

1 Like

Yes, you can replace it with that code. tell me if anything goes wrong or if you more help.

1 Like

It worked! Thank you so much!!!