How can I set the occlusion mode of the players camera in a script

Currently, I would like to use the camera occlusion modes Zoom and Invisicam both at different times. This can be set through the property in StarterPlayer.

For example:

When inside a building, use Invisicam
When I go outside that building, use Zoom, 
When I enter another building, go back to Invisicam
When I go out of that building, go back to Zoom.

However, I am not aware of any way to set it with a script. How can I set the occlusion mode to either zoom or invisicam in a script by any means possible?

2 Likes

Use:
player.DevCameraOcclusionMode

Examples:
player.DevCameraOcclusionMode = Enum.DevCameraOcclusionMode.Invisicam
player.DevCameraOcclusionMode = Enum.DevCameraOcclusionMode.Zoom
https://developer.roblox.com/en-us/api-reference/property/StarterPlayer/DevCameraOcclusionMode

2 Likes

If you set the property on a starterplayer, a player has to rejoin for it to change. Setting DevCameraOcclusionMode does not work because localscripts do not have the permission to change DevCameraOcclusionMode. This unfortunetly doesnt work.

1 Like

Apologies, I had forgotten that Roblox decides to block access to certain studio features.

You could instead copy the PlayerModule during runtime and paste it back into StarterPlayerScripts, then from there you can edit the Popper module. The Popper module is what controls camera occlusion.

Or, if you want to follow a more external approach, I’ve experimented with ray occlusion in the past. Set up a RenderStepped that runs Camera:GetPartsObscuringTarget(). This is the same approach the Popper module uses.

The dev API even includes a handy function:

local function XRay(castPoints, ignoreList)
	ignoreList = ignoreList or {}
	local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)
 
	for _, part in pairs(parts) do
		part.LocalTransparencyModifier = 0.75
		for _, child in pairs(part:GetChildren()) do
			if child:IsA("Decal") or child:IsA("Texture") then
				child.LocalTransparencyModifier = 0.75
			end
		end
	end
end

The only cast point should be {char.PrimaryPart.Position} (casts ray from cam to character)
Setting the local transparency to .25 (or greater) will allow the camera to clip through.

Edit: Make sure the character is inside the ignore list.
Hope this helps! :slight_smile:

6 Likes

sorry for the necropost, but there is a way to do this.

game.Players.PlayerAdded:Connect(function(plr)
	
	repeat 
		plr.DevCameraOcclusionMode = Enum.DevCameraOcclusionMode.Invisicam 
		task.wait(0.05) 
	until 
		plr.DevCameraOcclusionMode == Enum.DevCameraOcclusionMode.Invisicam
end)

the camera takes a slightly longer amount of time in order to fully load, so this will make sure of that.