HeadCamera Effect Isssue

So basically I am trying to make a HeadCamera like the one in the video shown below.

But I don’t really know how to make it lock, or how to hide the hats so you can see it normally.

I tryed using CFrames to make it so it work go into the head but it would only tp to the head once and stay there (even if the player was still moving)

local Players = game:GetService("Players")
local playerLabel = script.Parent:WaitForChild("PlayerLabel")
local uis = game:GetService("UserInputService")
local cam = workspace.CurrentCamera

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local isSpectating = false
local currentIndex = 1
local playerList = {}



local function updatePlayers()
	local newPlayerList = {}
	
	for plrs, plr in pairs(Players:GetPlayers()) do
		if plr ~= game.Players.LocalPlayer then
			table.insert(newPlayerList, plr.Name)
		end
	end
	
	playerList = newPlayerList
end


local function updateSpectator()
	local foundPlayer = Players:FindFirstChild(playerList[currentIndex])
	
	if foundPlayer then
		playerLabel.Text = foundPlayer.Name 
		cam.CameraSubject = foundPlayer.Character.Head
	end
end


local function nextPlayer()
	if currentIndex < #playerList then
		currentIndex += 1
	else
		currentIndex = 1
	end
	updateSpectator()
end


local function lastPlayer()
	if currentIndex > 1 then
		currentIndex -= 1
	else
		currentIndex = #playerList
	end
	
	updateSpectator()
end


local function quitSpectating()
	cam.CameraSubject = hum
	currentIndex = 1
end

hum.Died:Connect(function(player)
	script.Parent.static.Enabled = true
	script.Parent.Parent.Enabled = true
	isSpectating = not isSpectating
	script.Parent.Visible = isSpectating

	if isSpectating then
		updatePlayers()
		updateSpectator()
	else
		quitSpectating()
	end
end)

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Left then
		lastPlayer()
	end
	if input.KeyCode == Enum.KeyCode.Right then
		nextPlayer()
	end
end)

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.ButtonL1 then
		lastPlayer()
	end
	if input.KeyCode == Enum.KeyCode.ButtonR1 then
		nextPlayer()
	end
end)

Two videos
Uploading: 2023-06-14 11-00-53.mp4…
Uploading: 2023-06-15 20-17-52.mp4…
sorry if there is sensitive stuff in the video of Ready Or Not

The camera stays there and doesn’t move with the player head because you are just moving it there, not keeping it there.
You can resolve this issue by looping the function, with a coroutine or a spawn(function().

Something like:

spawn(function()
if spectating then --we will turn this off later so this doesn't repeat forever
camera.CFrame = player_to_spec.Head.CFrame --setting the camera continuously
task.wait(.1) --wait a bit so we don't crash the game
end
end)

And when we’re done spectating we set spectating to false, I recommend making this a BoolValue instead of just a script variable as it is easier to work with even from other scripts.

I don’t really get how to use spawn function. I tried this but it doesn’t loop it just sets it one time.

Where am I supposed to put the spawn function thing?

local function updateSpectator()
	local foundPlayer = Players:FindFirstChild(playerList[currentIndex])
	
	if foundPlayer then
		playerLabel.Text = foundPlayer.Name 
		spawn(function()
			if isSpectating then
				cam.CFrame = foundPlayer.Character.Head.CFrame
				task.wait(.1)
			end
		end)
	end
end

nvm i fixed it with a repeat loop

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.