This spectate system won't work when the player's far away

When the players are on the map and far away, because of streaming enabled it stops working sometimes. I even used the RequestStreamAroundAsync() but it still sometimes don’t work!

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local plr = players.LocalPlayer

local gui = script.Parent
local mainFrame = gui.MainFrame
local plrName = mainFrame.PlayerName
local nex = mainFrame.Next
local prev = mainFrame.Previous
local exit = mainFrame.Parent.Exit

local num = 1
local isSpectating = false -- Tracks if the player is in spectate mode

-- Function to check if a player is spectatable
local function IsValidSpectateTarget(targetPlayer)
	if targetPlayer and targetPlayer.Character and targetPlayer.Character:FindFirstChild("HumanoidRootPart") then
		return true
	end
	return false
end

-- Function to switch camera to a player
local function SetCam(n)
	local allPlayers = players:GetPlayers()
	if #allPlayers == 1 then
		warn("No available players to spectate.")
		return
	end

	num = n

	local targetPlayer = allPlayers[num]
	if not IsValidSpectateTarget(targetPlayer) then
		warn("Invalid spectate target. Finding next player...")
		num = (num % #allPlayers) + 1 -- Move to next player
		SetCam(num)
		return
	end

	local hrp = targetPlayer.Character:FindFirstChild("HumanoidRootPart")
	if not hrp then return end

	-- Ensure area is streamed before setting the camera
	local success, err = pcall(function()
		plr:RequestStreamAroundAsync(hrp.Position)
	end)

	if success then
		plrName.Text = targetPlayer.Name
		workspace.CurrentCamera.CameraSubject = targetPlayer.Character:FindFirstChild("Humanoid")
	else
		warn("Streaming failed: " .. (err or "Unknown error"))
		workspace.CurrentCamera.CFrame = CFrame.new(hrp.Position + Vector3.new(0, 10, 0), hrp.Position)
	end
end

-- Function to enter spectate mode
local function EnterSpectateMode()
	isSpectating = true
	gui.MainFrame.Visible = true

	local allPlayers = players:GetPlayers()
	num = 1

	-- Find first valid player
	for i, p in ipairs(allPlayers) do
		if p ~= plr and IsValidSpectateTarget(p) then
			num = i
			break
		end
	end

	SetCam(num)
end

-- Function to exit spectate mode
local function ExitSpectateMode()
	isSpectating = false
	gui.MainFrame.Visible = false
	workspace.CurrentCamera.CameraSubject = plr.Character:FindFirstChild("Humanoid")
end

-- Spectate on death
plr.Character:WaitForChild("Humanoid").Died:Connect(function()
	if not isSpectating then
		EnterSpectateMode()
	end
end)

-- Spectate when using the TV ProximityPrompt
workspace:WaitForChild("Lobby"):WaitForChild("TV").Screen.ProximityPrompt.Triggered:Connect(function()
	if not isSpectating then
		EnterSpectateMode()
		workspace.Lobby.TV.Screen.ProximityPrompt.Enabled = false
		exit.Visible = true
	end
end)

-- Handle navigation between players
prev.MouseButton1Click:Connect(function()
	num = (num - 2) % #players:GetPlayers() + 1
	SetCam(num)
end)

nex.MouseButton1Click:Connect(function()
	num = num % #players:GetPlayers() + 1
	SetCam(num)
end)

exit.MouseButton1Click:Connect(function()
	if isSpectating then
		workspace.Lobby.TV.Screen.ProximityPrompt.Enabled = true
		ExitSpectateMode()
		exit.Visible = false
	end
end)

1 Like

When you say far away, how far are we talking? This sounds like the floating point error bug to me. There’s nothing that can fix this if it’s 2000 studs away or more

how to check that, cuz idk how to check that?

change this to

warn("Streaming failed: " .. hrp.Position))

Reply back with the position it shows