(Spectate system) TextButton setting workspace.Camera to Players[(i % #Players) + 1].HumanoidRootPart.CFrame Not working BUG

Whenever I Activate the button, it keeps setting my workspace.Camera.Cframe to my own Character.HumanoidRootPart.CFrame

Script :

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	if Hit then
		local Players = game.Players:GetPlayers()
		local CurrentPlayer = game.Players.LocalPlayer
		local NextPlayer

		for i, Player in ipairs(Players) do
			if Player ~= CurrentPlayer then
				NextPlayer = Players[(i % #Players) + 1]
				break
			end
			return NextPlayer
		end

		if NextPlayer then
			local NextPlayerCharacter = NextPlayer.Character
			if NextPlayerCharacter then
				local HRP = NextPlayerCharacter:FindFirstChild("HumanoidRootPart")
				if HRP then
					workspace.CurrentCamera.CFrame = CFrame.new(HRP.Position)
				end
			end
		end
	end
end)
  1. Make sure there is more than 1 player in game

return instantly ends the thread, preventing code after it from running. Since you already used break when the next player is found, this is unnecessary.
3.

This doesn’t modify the camera (at least in Custom CameraType), you need to change it’s CameraSubject instead:

if NextPlayerCharacter then
	workspace.CurrentCamera.CameraSubject = NextPlayerCharacter.Humanoid
end
1 Like
local Button = script.Parent

Button.Activated:Connect(function(Hit)
	if Hit then
		local Players = game.Players:GetPlayers()
		local CurrentPlayer = game.Players.LocalPlayer
		local NextPlayer

		for i, Player in ipairs(Players) do
			if Player ~= CurrentPlayer then
				NextPlayer = Players[(i % #Players) + 1]
				break
			end
		end

		if NextPlayer then
			local NextPlayerCharacter = NextPlayer.Character
			if NextPlayerCharacter then
				local Humanoid = NextPlayerCharacter:FindFirstChild("Humanoid")
				if Humanoid then
					workspace.CurrentCamera.CameraSubject = CFrame.new(Humanoid.Position)
				end
			end
		end
	end
end)

I’ve made the changes and it’s still not working

CameraSubject takes the Humanoid instance (like above), not a CFrame

1 Like
local Button = script.Parent

Button.Activated:Connect(function(Hit)
	if Hit then
		local Players = game.Players:GetPlayers()
		local CurrentPlayer = game.Players.LocalPlayer
		local NextPlayer

		for i, Player in ipairs(Players) do
			if Player ~= CurrentPlayer then
				NextPlayer = Players[(i % #Players) + 1]
				break
			end
		end

		if NextPlayer then
			local NextPlayerCharacter = NextPlayer.Character
			if NextPlayerCharacter then
				local H = NextPlayerCharacter:FindFirstChild("Humanoid")
				if H then
					workspace.CurrentCamera.CameraSubject = H
				end
			end
		end
	end
end)

I’ve made those changes and it’s only working on the client itself

Fixed it.

local Button = script.Parent

Button.Activated:Connect(function(Hit)
	if Hit then
		local Players = game.Players:GetPlayers()
		local CurrentPlayer = game.Players.LocalPlayer
		local NextPlayer

		local Index
		for i, Player in ipairs(Players) do
			if Player == CurrentPlayer then
				Index = i
				break
			end
		end

		if Index then
			local Index2 = Index % #Players + 1
			NextPlayer = Players[Index2]
		end

		if NextPlayer then
			local NextPlayerCharacter = NextPlayer.Character
			if NextPlayerCharacter then
				local H = NextPlayerCharacter:FindFirstChild("Humanoid")
				if H then
					workspace.CurrentCamera.CameraSubject = H
				end
			end
		end
	end
end)

The previous code assigned the next player based on the index in the Players list (Which somehow caused an error)

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