Spectate GUI fix code [Obby]

Hello guys,

I adding here code for classic Spectate GUI:

local cam = game.Workspace.CurrentCamera

local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.SpectateButton

function get()
	for _,v in pairs(game.Players:GetPlayers())do
		if v.Name == title.Text then
			return(_)
		end
	end
end


local debounce = false
button.MouseButton1Click:connect(function()
	if debounce == false then debounce = true
		bar:TweenPosition(UDim2.new(0, 0, 0, 0),"In","Linear",1,true)
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	elseif debounce == true then debounce = false
		pcall(function() cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid end)
		bar:TweenPosition(UDim2.new(1, 0, 0, 0),"In","Linear",1,true)
	end
end)

prev.MouseButton1Click:connect(function()
	wait(.1)
	local players = game.Players:GetPlayers()
	local num = get()
	if not pcall(function() 
			cam.CameraSubject = players[num-1].Character.Humanoid
		end) then
		cam.CameraSubject = players[#players].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

nex.MouseButton1Click:connect(function()
	wait(.1)
	local players = game.Players:GetPlayers()
	local num = get()
	if not pcall(function() 
			cam.CameraSubject = players[num+1].Character.Humanoid
		end) then
		cam.CameraSubject = players[1].Character.Humanoid
	end
	pcall(function()
		title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
	end)
end)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:lower() == "e" or key:upper() == "E" then
		wait(.1)
		local players = game.Players:GetPlayers()
		local num = get()
		if not pcall(function() 
				cam.CameraSubject = players[num+1].Character.Humanoid
			end) then
			cam.CameraSubject = players[1].Character.Humanoid
		end
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	end
end)


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key:lower() == "q" or key:upper() == "Q" then
		wait(.1)
		local players = game.Players:GetPlayers()
		local num = get()
		if not pcall(function() 
				cam.CameraSubject = players[num-1].Character.Humanoid
			end) then
			cam.CameraSubject = players[#players].Character.Humanoid
		end
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	end
end)

Can someone add to this code function when player what i spectate “die/respawned” camera is back to him after and not stay in same position when “he” die? :slight_smile:

Thanks for all help!

2 Likes

Try adding this to your code:

local char;

repeat 
wait(0.1)
until
player.Character ~= nil

char = player.Character

local humd = char:WaitForChild("Humanoid")

humd.Died:Connect(function()
cam.CameraSubject = humd
end)
3 Likes

Nope this not work, tryed … some other idea?

Do you have any errors? Try printing every line and see where it breaks.

[10:05:49.625 - Workspace.HeadshotAI.SpecDie:6: attempt to index nil with 'Character'](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7B1F96FD39-54D0-4BBB-9C7E-01AFBDFF6786%7D&gst=2#6)

10:05:49.626 - Stack Begin

[10:05:49.626 - Script 'Workspace.HeadshotAI.SpecDie', Line 6](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7B1F96FD39-54D0-4BBB-9C7E-01AFBDFF6786%7D&gst=2#6)

10:05:49.626 - Stack End

And have PUT this in “StarterCharacterScripts”

Hey Still need fix this code.
Someone know how to fix looping spectate same player if player what i watch die???
When player die camera stay in position where is player die but not go back to him.