Light brightness issue

  1. What do you want to achieve?
    like real eye-adaptive brightness from light to dark

  2. What’s the problem?
    there will be obvious flickering problems on the ground

  3. What solutions have you tried so far?
    I try but when it reaches 7 it fails he doesn’t stop but continues

local light = game:GetService("Lighting")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local areasGroup = workspace:WaitForChild("light")

humanoid.Touched:Connect(function(touchedPart)
	if touchedPart.Parent == areasGroup then
		repeat light.Brightness = light.Brightness + 0.1 wait(0.01) until light.Brightness == 7
	touchedPart.TouchEnded:Connect(function(touchEndedPart)
		repeat light.Brightness = light.Brightness - 0.1 wait(0.01) until light.Brightness == 3
	end)    
end
end)

currently used script

local light = game:GetService("Lighting")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local areasGroup = workspace:WaitForChild("light")

humanoid.Touched:Connect(function(touchedPart)
	if touchedPart.Parent == areasGroup then
		for i= 3, 7, 0.1 do
			light.Brightness = i
			wait(0.01)
		end
	touchedPart.TouchEnded:Connect(function(touchEndedPart)
		for i= 7, 3, -0.1 do
			light.Brightness = i
			wait(0.01)
		end
	end)    
end
end)

Location: It’s a localscript in StarterCharacterScripts

1 Like

try

if touchedPart.Parent == areasGroup then
		repeat light.Brightness = light.Brightness + 0.1 wait(0.01) until light.Brightness >= 7
	touchedPart.TouchEnded:Connect(function(touchEndedPart)
		repeat light.Brightness = light.Brightness - 0.1 wait(0.01) until light.Brightness >= 3
	end)

Thanks for your reply, but when he entered he suddenly became unlimited reduction

1 Like

you probably want to add a debounce, such as

local brightnessDebounce = false
humanoid.Touched:Connect(function(touchedPart)
if brightnessDeboucne then return end
if touchedPart.Parent == areasGroup then
        brightnessDebounce = true
		repeat light.Brightness = light.Brightness + 0.1 wait(0.01) until light.Brightness == 7
	touchedPart.TouchEnded:Connect(function(touchEndedPart)
		repeat light.Brightness = light.Brightness - 0.1 wait(0.01) until light.Brightness == 3
        brightnessDebounce = false
	end) 
end
end)

basically debounce is for
only run once, and when it is done animating, then let it be able to run again


thank for your support. After hours of debug there is one more problem that doesn’t diminish when leaving

local light = game:GetService("Lighting")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local areasGroup = workspace:WaitForChild("light")
local brightnessDebounce = false

humanoid.Touched:Connect(function(touchedPart)
	if brightnessDebounce then return end
	if touchedPart.Parent == areasGroup then
		brightnessDebounce = true
		if brightnessDebounce == true then
		repeat light.Brightness = light.Brightness + 0.1 wait(0.01) until light.Brightness >= 7
		end
		if brightnessDebounce >= 7 then
			brightnessDebounce = false
		end
		touchedPart.TouchEnded:Connect(function(touchEndedPart)
			brightnessDebounce = true
			if brightnessDebounce == true then
				repeat light.Brightness = light.Brightness - 0.1 wait(0.01) until light.Brightness <= 3
			end
			if brightnessDebounce <= 3 then
				brightnessDebounce = false
			end
		end)    
	end
end)
1 Like

You should also consider not using humanoid.Touched and just
touchedPart.Touched
touchedPart.TouchEnded

otherwise you’re connecting a new event to touchedPart each time a humanoid touches it.

sorry I don’t understand

put touchedPart in a variable, you need to index through workspace to find it, then the parameter for the function should be Hit and then you can use Hit to find if it is a humanoid that has touched it and then check if the humanoid is the player and then do your brightness code

1 Like

Why not use TweenService?

local TweenService = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")

function setLightBrightness(brightness: number)
    local tween = TweenService:Create(
        lighting,
        TweenInfo.new(10, Enum.EasingStyle.Linear),
        {Brightness = brightness}
    )
    
    tween:Play()
end

See TweenService.

1 Like

thank it got fix all good

1 Like

Thanks the problem has been solved

1 Like