Remote event only firing when game window is unfocused

  1. What do you want to achieve?

I have 2 camera scripts that has the logic for 2 different camera parts in my game, that are facing each other. There are also some models that get hidden for each player. (only 2 players btw)

  1. What is the issue?

I’m gonna try to explain this as best as possible- If press the test button and I’m focused in the game window, the script does not work. While the game is loading, I unfocused the game window and it worked.

  1. What solutions have you tried so far?

I’ve tried some AI’s (I’m actually not very good at scripting) and it didn’t really help.
LocalScript in StarterPlayerScripts:

print("initialized")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CameraAssignmentEvent = ReplicatedStorage:WaitForChild("CameraAssignmentEvent")

local player = Players.LocalPlayer
local assignedCameraPartName
local modelToHideName

local deadZoneSize = Vector2.new(0, 0) -- Size of the dead zone in pixels (adjust as needed)
local maxAngle = 40 -- Maximum angle for camera rotation
local sensitivity = 7 -- Camera sensitivity

-- Function to check if the mouse is in the dead zone
local function isInDeadZone(mousePosition, screenSize)
	local screenCenter = screenSize / 2
	local distance = (mousePosition - screenCenter).Magnitude
	return distance < deadZoneSize.Magnitude
end

-- Update camera based on the mouse position
local function updateCamera()
	local Mouse = player:GetMouse()
	local Camera = workspace.CurrentCamera
	local screenSize = Camera.ViewportSize
	local mousePosition = Vector2.new(Mouse.X, Mouse.Y)

	-- Debugging: Print mouse position
	print("Mouse Position:", mousePosition)

	if assignedCameraPartName and not isInDeadZone(mousePosition, screenSize) then
		local assignedCameraPart = workspace:FindFirstChild(assignedCameraPartName)
		if assignedCameraPart then
			local screenCenter = screenSize / 2
			local offset = (mousePosition - screenCenter) / screenSize * 2 * sensitivity
			offset = Vector2.new(math.clamp(offset.X, -maxAngle, maxAngle), math.clamp(offset.Y, -maxAngle, maxAngle))

			local rotationX = math.rad(-offset.Y)
			local rotationY = math.rad(-offset.X)

			local baseCFrame = CFrame.lookAt(assignedCameraPart.Position, assignedCameraPart.Position + assignedCameraPart.CFrame.LookVector)
			local adjustedCFrame = CFrame.Angles(rotationX, rotationY, 0)
			Camera.CFrame = baseCFrame * adjustedCFrame

			-- Debugging: Print camera update status
			print("Updating camera for part:", assignedCameraPartName)
		end
	end
end

-- Function to hide the model
local function hideModel(modelName)
	local model = workspace:FindFirstChild(modelName)
	if model then
		model:Destroy() -- Or set its visibility off
		-- Debugging: Print model hiding status
		print("Hiding model: " .. modelName)
	else
		print("Model not found: " .. modelName)
	end
end

-- Listen for camera part assignment
CameraAssignmentEvent.OnClientEvent:Connect(function(assignedPartName, hideModelName)
	assignedCameraPartName = assignedPartName
	modelToHideName = hideModelName
	print("Received camera assignment for: " .. assignedPartName)
	print("Model to hide: " .. hideModelName)
	wait(1) -- Wait for the models to load properly
	hideModel(modelToHideName)
end)

-- Main update loop
RunService.RenderStepped:Connect(updateCamera)

ServerScript in ServerScriptService:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CameraAssignmentEvent = Instance.new("RemoteEvent", ReplicatedStorage)
CameraAssignmentEvent.Name = "CameraAssignmentEvent"

local CameraP1 = workspace:WaitForChild("CameraPart1")
local CameraP2 = workspace:WaitForChild("CameraPart2")

local playerCameraAssignments = {}

local function assignCameraToPlayer(player)
	if not playerCameraAssignments[CameraP1] then
		playerCameraAssignments[CameraP1] = player
		CameraAssignmentEvent:FireClient(player, CameraP1.Name, "Jack1")
		print("Assigned CameraP1 to " .. player.Name)
	elseif not playerCameraAssignments[CameraP2] then
		playerCameraAssignments[CameraP2] = player
		CameraAssignmentEvent:FireClient(player, CameraP2.Name, "Jack2")
		print("Assigned CameraP2 to " .. player.Name)
	end
end

for _, player in ipairs(Players:GetPlayers()) do
	assignCameraToPlayer(player)
end

Players.PlayerAdded:Connect(assignCameraToPlayer)

thanks for the help!