How exactly would I send camera through remote events?

I’m a little bit confused on how I would send the player’s camera to a server script with remote events, how would I go about doing this?

1 Like

You can’t really send the actual Camera through remotes, but you can communicate its local properties to the server through remotes.

What information do you require from the camera to be sent to the server?

3 Likes

The camera’s CFrame, could I send that?

1 Like

yes, you can, you can’t send the local camera bc you can only manipulate it from the client

Of course, a basic setup would be like so:

-- Client Side (LocalScript)
local Camera = workspace.CurrentCamera
local Remote = game.ReplicatedStorage.RemoteEvent

-- Continuously communicate Camera.CFrame to the server
game:GetService('RunService').Stepped:Connect(function()
    Remote:FireServer(Camera.CFrame)
end)
-- Server Side (Script)
local Remote = game.ReplicatedStorage.RemoteEvent

Remote.OnServerEvent:Connect(function(Player, CamCFrame)
    -- CamCFrame is Player's camera CFrame
end)
1 Like

I’d suggest using .RenderStepped instead of .Stepped as the camera gets updated per frame not per physics step.

I hypothesised Stepped would be a better option in order to not spam the remote too much, and unintentionally use too of the server’s memory (if 20 players in a server were sending their Camera’s CFrame @ 60 FPS, I believe it’d be an optimisation concern). Is this unnecessary?

Oh yeah, how would I send the middle mouse button to a server script aswell? It’s something else for my game. (Sorry for going a bit off-topic)

You mean the position of the mouse in 2D on the screen (in pixels) or the CFrame of the mouse in 3D space?

No, the middle mouse button, when a player presses the middle mouse button, how would I send it?

You can use UserInputService’s InputBegan Event. If you listen for the UserInputType.MouseButton3, you can connect your function to that. For example:

-- Services
local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:Connect(function(Input, GPE)
	local UserInputType = Input.UserInputType
	
	if UserInputType == Enum.UserInputType.MouseButton3 then
		-- Execute logic when player presses the middle mouse button
	end
end)
local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local UserInputService = game:GetService("UserInputService")

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton3 then
       game.ReplicatedStorage.RemotEvent:FireServer(Camera.CFrame)
    end
end)

Out of pure curiosity, why are you calling for a reference to Player:GetMouse() in this case? I didn’t think it was necessary, but I may have made a horrible oversight :cold_sweat:

:joy: Just used to writing those 3 lines ahah

1 Like

How would I give the server the key of mouse3? For example with key codes you can do
Remote:FireServer(UIS:GetStringForKeyCode(Input.KeyCode)) how would I be able to do this with mouse3?

MouseButton3 doesn’t have a keycode as it’s not a key, KeyCodes only apply to keys on the keyboard

The only way you can really tell the server the player has pressed the middle mouse button is by firing a RemoteEvent specifically for that purpose

-- Services
local UserInputService = game:GetService('UserInputService')

-- Hypothetical RemoteEvent called 'UserInput'
local UserInputRemote = game.ReplicatedStorage.UserInput

UserInputService.InputBegan:Connect(function(Input, GPE)
	local UserInputType = Input.UserInputType

	if UserInputType == Enum.UserInputType.MouseButton3 then
		UserInputRemote:FireServer('MouseButton3')
	end
end)

Does this make sense?

What would the server script look like?

-- Hypothetical RemoteEvent called 'UserInput'
local UserInputRemote = game.ReplicatedStorage.UserInput

UserInputRemote.OnServerEvent:Connect(function(Player, Input)
    if Input == 'MouseButton3' then
        -- Logic for if a player has pressed the middle mouse button
    end
end)

One last question, how would I detect if they pressed MouseButton3 or the “L” key on a keyboard?

Using the UserInputService.InputBegan Event, you can fetch the KeyCode and send that through the UserInputRemote

E.G:

-- Services
local UserInputService = game:GetService('UserInputService')

-- Hypothetical RemoteEvent called 'UserInput'
local UserInputRemote = game.ReplicatedStorage.UserInput

UserInputService.InputBegan:Connect(function(Input, GPE)
	local KeyCode, UserInputType = Input.KeyCode, Input.UserInputType

	if UserInputType == Enum.UserInputType.Keyboard then
		local Key = UserInputService:GetStringForKeyCode(KeyCode)
		UserInputRemote:FireServer(Key)
	end
end)

Now you just have to combine them :grin: Best of luck!