Distance Calculation Methods

Current Motives

Currently, I am thinking about the best method of calculating the player’s distance to a part to be able to tone down the client’s lighting using the ExposureCompensation property in Lighting. If you are wondering for the purpose of using this property, it will be for SCP 1008. Basically, how I want it to work is so if the player is close enough to the SCP, the effects of SCP 1008 will start to affect the player, however, there will be a debounce. However, I have concerns on whether the RenderStepped event may result in too many resources being put on the client and may also result in functions constantly ran on the client. If there are BETTER WAYS of doing this. Thank you for reading this far.

local debounce = false
local part = workspace:WaitForChild("Part")

local MAX_DIST = 50

local plrs = game:GetService("Players")
local rs = game:GetService("RunService")

local p = plrs.LocalPlayer

rs.RenderStepped:Connect(function()
    if distance <= 5 then
	if debounce then return end
	local lighting = game:GetService("Lighting").ExposureCompensation
	local text = "You suddendly begin to experience everything go brighter.."
	local pgui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
	local label = pgui.SCPGui.SCP
	debounce = true
	for i = 1, #text do
		label.Text = string.sub(text, 1, i)
		wait(0.05)
	end
	for i = 1, 100 do
		lighting = lighting + 0.01
		wait(0.01)
	end
	debounce = false
	if distance >= 5 then
		if debounce then return end
		text = "You suddendly begin to experience everything go brighter.."
		debounce = true
		for i = 1, #text do
		label.Text = string.sub(text, 1, i)
		wait(0.05)
	end
	for i = 1, 100 do
		lighting = lighting - 0.01
		wait(0.01)
	end
	debounce = false
	end
end
end)

Running this code in RenderStepped will drastically affect performance due to this reason: All code bound to RenderStopped must finish running before the next frame. All of your wait statements will result in multiple seconds passing before each new frame. A better way of handling this would be binding this to Stepped or Heartbeat and utilizing TweenService for tweening ExposureCompensation (also, wherever you’re adding 0.01 to lighting will not change ExposureCompensation, you would need to assign that to ExposureCompensation directly since lighting does NOT reference it).

1 Like

That’s perfect. Thank you @wow13524!