Spectate System can't switch between players

It’s not switching between players (stuck to one player). I’m not sure what the issue actually is, but probably something wrong with the logic

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

		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("HumanoidRootPart")
				if H then
					workspace.CurrentCamera.CameraSubject = H
				end
			end
		end
	end
end)

Is there only one Button to switch between players?

I see why it is not changing, you did not set the camera type of the camera to Enum.CameraType.Scriptable!!!

Everytime player clicks the button, the current player is set to the player itself. So players[index + 1] will always return the next player who joined after the local player did. To fix this you simply store currentPlayer variable outside the function and update it when the player wants to change spectate target.

Rewrite:

local playersService = game:GetService("Players")

local button = script.Parent

local camera = game:GetService("Workspace").CurrentCamera
local currentPlayer = playersService.LocalPlayer

local function spectate()
	local character = currentPlayer.Character
	if not character then return end
	
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return end
	
	camera.CameraSubject = humanoidRootPart
end

button.MouseButton1Click:Connect(function()
	local players = playersService:GetPlayers()
	local currentIndex = table.find(players, currentPlayer)
	local nextIndex = currentIndex % #players + 1
	local nextPlayer = players[nextIndex]
	
	currentPlayer = nextPlayer
	spectate()
end)
1 Like

There’s another button with it’s own localscript with for i = #Players, 1, -1 do

local Button = script.Parent
local Camera = workspace.CurrentCamera

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("HumanoidRootPart")
				if H then
					Camera.CameraType = Enum.CameraType.Scriptable
					Camera.CameraSubject = H
				end
			end
		end
	end
end)

I’ve changed it and it’s not really working

Thank you so much!! This is working, ty!!!

1 Like

Don’t do that. You can combine both functionalities in one script.

local playersService = game:GetService("Players")

local leftButton = script.Parent.LeftButton
local rightButton = script.Parent.RightButton

local camera = game:GetService("Workspace").CurrentCamera
local currentPlayer = playersService.LocalPlayer

local function spectate()
	local character = currentPlayer.Character
	if not character then return end
	
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return end
	
	camera.CameraSubject = humanoidRootPart
end

local function changeSpectatingPlayer(direction)
	local players = playersService:GetPlayers()

	local currentIndex = table.find(players, currentPlayer)
	local nextIndex = (currentIndex + index) % #players
    if nextIndex == 0 then nextIndex = #players end

    currentPlayer = players[nextIndex]
    spectate()
end

leftButton.MouseButton1Click:Connect(function()
    changeSpectatingPlayer(-1)
end)

rightButton.MouseButton1Click:Connect(function()
   changeSpectatingPlayer(1)
end)
1 Like

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