Issues with Camera Script

Why are cameras so hard to use :sweat:

So, I’m trying to use this script to update the player’s camera because the character is updated. It doesn’t work.

Scripts

Camera Script (Local)

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = game.Workspace.CurrentCamera  -- always use CurrentCamera to reference the camera
local tool = script.Parent
tool.MouseButton1Click:Connect(function()
	if workspace.CurrentCamera and player.Character then
		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			workspace.CurrentCamera.CameraSubject = humanoid
		end
	end
end)

Morph Script (Server)

script.Parent.MouseButton1Click:Connect(function()
	local plr = script.Parent.Parent.Parent.Parent.Parent
	print(plr.Name .. " morph to ralsei yayaya")
	game.Lighting.Ralsei.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
	local clone = game.Lighting.Ralsei:Clone()
	clone.Parent = game.Workspace
	clone.Name = "Ralsei (".. plr.Name .. ")"
	plr.Character = clone
end)

I don’t think MouseButton1Click works on the server it only works on the client

Tools don’t have MouseButton1Click, but you can use Activated which is basically MouseButton1Click but for tools

This is a GUI. That’s all I have to say.

I’ve used it serverside before so I don’t see why it wouldn’t work. Anyways, I said the camera was not working, the morph works fine.

The camera is being set before the morph is set due to latency, you could possibly do a remotefunction which will yield until you return and then the camera can be set.

print("a")
game:GetService("ReplicatedStorage").RemoteFunction:InvokeServer()
print("b")
game:GetService("ReplicatedStorage").RemoteFunction.OnServerInvoke = function()
--stuffhere
return
end

“a” will be printed and then it will yield for the server until it returns and then “b” will be printed afterwards

1 Like

Do I have to use :GetService? It seems pointless as it’s a main service you can find in the Explorer.

You don’t have to use :GetService but it is preferred incase the service names have been changed.
It is just a preference.

1 Like

I’ve updated the code and it still doesn’t seem to work.

Local Script

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = game.Workspace.CurrentCamera  -- always use CurrentCamera to reference the camera
local tool = script.Parent
tool.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RalseiCamFunction:InvokeServer()
	if workspace.CurrentCamera and player.Character then
		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			workspace.CurrentCamera.CameraSubject = humanoid
		end
	end
end)
game.ReplicatedStorage.RalseiCamFunction.OnClientInvoke:Connect(function()
	game.ReplicatedStorage.RalseiCamFunction:InvokeServer()
end)

Server Script

script.Parent.MouseButton1Click:Connect(function()
	finished = false
	local plr = script.Parent.Parent.Parent.Parent.Parent
	print(plr.Name .. " morph to ralsei yayaya")
	game.Lighting.Ralsei.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
	local clone = game.Lighting.Ralsei:Clone()
	clone.Parent = game.Workspace
	clone.Name = "Ralsei (".. plr.Name .. ")"
	plr.Character = clone
end)
game.ReplicatedStorage.RalseiCamFunction.OnServerInvoke:Connect(function()
	if finished == true then
		return
	else
		game.ReplicatedStorage.RalseiCamFunction:InvokeClient()
	end
end)

Had you make sure the camera type is scriptable?

The roblox devforum article about CameraSubject didn’t at all mention it needing to be scriptable.

You confused RemoteFunctions with RemoteEvents

Local Script

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = game.Workspace.CurrentCamera  -- always use CurrentCamera to reference the camera
local tool = script.Parent
tool.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RalseiCamFunction:InvokeServer()
	if workspace.CurrentCamera and player.Character then
		local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			workspace.CurrentCamera.CameraSubject = humanoid
		end
	end
end)

Server Script

game.ReplicatedStorage.RalseiCamFunction.OnServerInvoke = function(plr)
	print(plr.Name .. " morph to ralsei yayaya")
	game.Lighting.Ralsei.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
	local clone = game.Lighting.Ralsei:Clone()
	clone.Parent = game.Workspace
	clone.Name = "Ralsei (".. plr.Name .. ")"
	plr.Character = clone
    return true
end
1 Like

If you are scripting the camera through a script, you must set the camera type to scriptable for it to work.

The CameraType doesn’t need to be set to Scriptable as it is a character. So it should stay as custom

Then what is the point of this script?

The point of the script is to set the camera’s subject when the character is entirely replaced with a morph.

Then the camera type should be scriptable, it enables the camera to be able to stuffs through scripting.

Scriptable is only for if you want to set the CFrame of the camera. What the OP is doing here is just setting the CameraSubject to the new character’s humanoid. Setting it to scriptable would not let the player rotate their camera.

The character is changed but the new one is now also a character so it doesn’t need to be scriptable because you are setting it to the character.

Oh I see, thanks for correcting my mistake.

1 Like