Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Camera not working on playeradded event?

I am trying to make it so that when the player joins the game, their camera's CFrame is set to a part in the workspace. And the player's walkspeed and jumppower is 0.

Here is what I did (normal script in serverscriptservice) :

local WorkspaceCamera = game.Workspace.StartCamera -- This is the part whom the camera should be attached to.

local Camera = workspace.CurrentCamera

game.Players.PlayerAdded:Connect(function(Player)
	
	print("Player Added!")
	
	Player.TeamColor = BrickColor.new("CGA brown")
	
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = WorkspaceCamera.CFrame
	
end)

The player's team changes properly. But not the walkspeed nor the jumppower nor the camera.

Why?

Thanks for any help.

2 comments

0

Error codes?

0

No errors. It shows player added from the print. But no errors.

2 answers

✓ Accepted answer

Well, here, I recommend you to set the CFrame of the Camera in Client side. I have tried testing your code, and it resulted in changing the CFrame of Camera on the Server. To fix this, you can do :

-- Server

--// Variables
local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("RemoteEvent") -- Example

--// Functions
local function OnAdded(player)
	player.TeamColor = BrickColor.new("CGA brown")

	repeat wait() until player.Character -- Waits for player's character to load in [Don't use CharacterAdded, as it will fire on player respawn]

	RE:FireClient(player)
end

--//Main
game.Players.PlayerAdded:Connect(OnAdded)

-- Local

--// Variables
local RS = game:GetService("ReplicatedStorage")
local RE = RS:WaitForChild("RemoteEvent") 
local cam = workspace.CurrentCamera
local setCam = workspace.StartCamera
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait() -- Waits for player's character to load in
local hum = char:WaitForChild("Humanoid")

--// Functions
local function OnEvent()
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CFrame = setCam.CFrame

	hum.WalkSpeed = 0
	hum.JumpPower = 0
end

--// Main
RE.OnClientEvent:Connect(OnEvent)

If you get stuck anywhere, or get confused, you can ask me in comments!

Lemme know if it helps!

You can't really access a player's camera through a server script. Put this in a LocalScript in the StarterPlayerScripts, and it should work:

local WorkspaceCamera = workspace.StartCamera --No need to use game.Workspace
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

--You may have to use a wait() here for 1-2 seconds because sometimes it breaks if it's not loaded fully yet

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = WorkspaceCamera.CFrame

player.Character.Humanoid.WalkSpeed = 0 --Disable walking
player.Character.Humanoid.JumpPower = 0 --Disable jumping

Now the problem is that you can't use LocalScript to change the player's Team, so you're going to need another (server/normal) script in the ServerScriptService with the following:

game.Players.PlayerAdded:Connect(function(player)
	
	player.TeamColor = BrickColor.new("CGA brown")
	
end)

Sorry for my English and let me know if I helped

This discussion is preserved as it was. New answers, comments, and votes are closed.