How to change camera CFrame based on teams

I’m making a clash royale type game. Right now I’m trying to make something like this: When 2 players join they get separated into 2 teams (blue and red). Then the blue team player gets the first camera and the red one gets the second camera.

I tried doing this for the blue one:

local Team = game.Teams.Blue:GetPlayers()
local Player = Team[math.random(1, #Team)]


if Player.Team.Name == "Blue" then
	workspace.Camera.CameraType = Enum.CameraType.Scriptable
	workspace.Camera.CFrame = workspace.BlueCam.CFrame
end

And this for the red one:

local Team = game.Teams.Red:GetPlayers()
local Player = Team[math.random(1, #Team)]


if Player.Team.Name == "Red" then
	workspace.Camera.CameraType = Enum.CameraType.Scriptable
	workspace.Camera.CFrame = workspace.RedCam.CFrame
end

But it’s not working. Both players are looking from the same camera. I don’t know what to do.

Are these seperate scripts?
Also, I’m not sure it would work for your situation but in general in would be easier to make one function and a single table with all of the players, inside of this function just parse trough each member of the table(players) with an if statement have the player assigned to a camera based on the name of their team.

I tried this and it didn’t work. I tested the game and the camera of the player from the red team was changed, but the camera of the player from the blue team was not changed. There are also no errors.

local Players = game.Players:GetPlayers()

local Table = {}

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

for _, player in ipairs(Players) do
	table.insert(Table, player)
end

for i, val in pairs(Table) do
	if val.Team.Name == "Blue" then
		workspace.CurrentCamera.CFrame = workspace.BlueCam.CFrame
	elseif val.Team.Name == "Red" then
		workspace.CurrentCamera.CFrame = workspace.RedCam.CFrame
	end
end

Is this a server script? If so, to change the player’s camera you need to do so in a local script.

what about the one I did right now? It’s a local script and it’s in StarterGui

where is this located? it has be located in each client. You cant loop thru all players, you have to update camera for every local player.

I made a new script for you, it should be a LocalScript located in any place where LocalScripts execute such as StarterPlayerScripts, StarterGui, ReplicatedFirst, etc.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local RedCam = workspace.RedCam
local BlueCam = workspace.BlueCam

function SetCamera(camera)
	workspace.Camera.CameraType = Enum.CameraType.Scriptable
	workspace.Camera.CFrame = workspace.RedCam.CFrame
end

if Player.Team.Name == "Red" then
	SetCamera(RedCam)
elseif Player.Team.Name == "Blue" then
	SetCamera(BlueCam)
end

Thank you so much! Also I changed workspace.Camera.CFrame = workspace.RedCam.CFrame to workspace.Camera.CFrame = camera.CFrame

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