Invisible part that changes atmosphere density only while touched


So I’m not that experienced in coding with Lua but I’m trying to do a local script where while you touch a part, the density of the Atmosphere goes from 0.75 (which is where it’ll usually be at) to 0.

What the children of Lighting look like

(The normal atmosphere density on 0.75 in game, the little yellow fog effect going on).

(How the density looks on 0.75 in the location I don’t want it to appear in) Not as creepy.

(What it would look like in the location if the density switches to 0, ONLY if touching the invisible part). WAY better as things now appear darker and less friendly.

I’d appreciate it with my entire soul if someone was kind enough to guide me on how I would go about this since the yellow fog effect throws away the sinister look in that spot.

Use Basepart’s .Touched and .TouchEnded event.

Oh that’s quite simple, I presume you’re going to use a region if the part goes over everywhere that should be dark

First, the localscript must be somewhere where it can run, I’d recommend StarterPlayerScripts.

Then, set up a region using that invisible part you’re mentioning, and every frame (if doesn’t cause problems), check if the player’s character is in the region, and if it is, change the density, otherwise, keep it at 0.75. For this, we’ll use FindPartsInRegion3WithWhiteList(), with it only caring for your character.

We’re going to use Region3s because TouchEnded is a bit weird sometimes

Here’s an example of what you could use

local Players = game:GetService("Players")
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local regionPart = --The part to use
local size = regionPart.Size * 0.5
local pos = regionPart.Position

local region = - size, pos + size)

while true do
	if not char then
		char = player.Character or player.CharacterAdded:Wait()
	local condition = #workspace:FindPartsInRegion3WithWhiteList(region, {char}) > 0 
	Lighting.Atmosphere.Density = condition and 0 or 0.75

Every frame it checks if the character doesn’t exist in the variable and if it doesn’t, it gets the character again and then checks if it found the character in that region and changes the density accordingly

Lighting.Atmosphere.Density = condition and 0 or 0.75

If the condition was true, then set density to 0, if it was false, set it to 0.75

You’;d have to change it around to include more than 1 zone

Although I think it would be the best approach to use ZonePlus v2 to determine if they’re in the zone or not


Thank you so much! This should come in clutch when I get back on studio later again :slight_smile: