First person camera spectating help

Hi, I want to know if this would be okay to use or not since it has something to do with firing to the server every time someone moves their mouse

First local script named PlayerDirection, under the StarterCharacterScripts:

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local CurrentCamera = game.Workspace.CurrentCamera

game.ReplicatedStorage.Player.Spectate.isSpectating:GetPropertyChangedSignal("Value"):Connect(function() -- Check if this value inside the ReplicatedStorage gets changed
	if game.ReplicatedStorage.Player.Spectate.isSpectating.Value == true then -- If its true, enable the script under it
		script.Specate.Enabled = true
	elseif game.ReplicatedStorage.Player.Spectate.isSpectating.Value == false then -- If its false, disable the script under it
		script.Specate.Enabled = false
	end
end)

player:GetMouse().Move:Connect(function() -- When the player moves their camera
	local cameraDirection = CurrentCamera.CFrame.LookVector -- Get the direction
	if script.Parent:FindFirstChild("isBeingSpectated").Value == true then -- If this value is true, send to the server the players direction
		game.ReplicatedStorage.Player.updatePlayerDirection:FireServer(cameraDirection)
	end
end)

Second local script named Spectate, under the first local script

local player = game.Players.LocalPlayer
local creator = tostring(workspace[player.Name].Humanoid:FindFirstChild("creator").Value) --Find the creator value inside the players humanoid

if creator ~= nil then -- If the creator value exists then
	workspace[creator].PlayerDirection:GetPropertyChangedSignal("Value"):Connect(function() -- Every time the PlayerDirection value changes
		workspace.CurrentCamera.CFrame = CFrame.new(workspace[creator]:WaitForChild("HumanoidRootPart").Position, workspace[creator]:WaitForChild("HumanoidRootPart").Position + workspace[creator].PlayerDirection.Value)
	end)
end

Script under ServerScriptService:

game.ReplicatedStorage.Player.updatePlayerDirection.OnServerEvent:Connect(function(player, cameraDirection) -- Update the PlayerDirection value 
	player.Character:WaitForChild("PlayerDirection").Value = cameraDirection
end)

game.ReplicatedStorage.Player.isBeingSpectated.OnServerEvent:Connect(function(player, Character, v) 
	if v == "True" then
		Character.isBeingSpectated.Value = true
	elseif v == "False" then
		Character.isBeingSpectated.Value = false
	end
end)

image

And for more context, there is a Vector3 value named PlayerDirection and a BoolValue named isBeingSpectated under StarterCharacterScripts.

Also the isBeingSpectated value gets changed if the player killed someone and that someone fired to the server to change it. The isSpectating value gets changed if you get killed.

Thanks.

1 Like