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)
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
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)
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
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)