Distance Between Player and Lights

Hello! I’ll cut right to the point. I’m trying to figure out and get examples at the best way to loop several SurfaceLights around a Player, and if they have a large distance from the player they will be turned off, and if they are within a closer distance they’ll turn on. This is to help and soften performance considering the absurd amount of lights there are within the area.

I’m not that familiar with CFrame and Vector3 and what not, so If I could get some help it’d be appreciated. Thanks!

Hey, I don’t know how to turn off but I guess you can do it.

local surfacelight = script.Parent

while wait(1) do
for i, players in pairs(game.Players:GetChildren()) do
if workspace[players.Name] then
local character = workspace[players.Name] 
if (surfacelight.Position - characters.HumanoidRootPart.Position).Magnitude < 25 then-- you can edit the 25
-- close the lights
end
end
end

Thanks for replying! So if I were to loop several surface lights, would I just want to do something like,

for i, v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("SurfaceLight") then
		code for toggling on & off
	end
end

You need the magnitude, you also can do it like
(inside the surfacelight if it is a physical thing)

for E,player in pairs(workspace:GetChildren()) do
while wait(1) do
if player:FindFirstChild("Humanoid") then
if(script.Parent.Position - player.HumanoidRootPart.Position).Magnitude > 25 then
-- script to close the lights
end
end
end
end

There are several SurfaceLights under individual parts with in the Workspace, and I mean several. I need to loop the entire Workspace for all Surface lights, check their distance from the player and then determine whether they’re suppose to be off or on.

Are the individual parts in a group or not? So I can rescript it to it.

Theres a SurfaceLight under every model that is named a light, and there are several of them. That’s why I’m catching descendants in the Workspace, because the SurfaceLight is under a part named “CoreLight”, so that I can grab the position of that part.

Sorry if I want many things :smiley:
Can you send me the explorer? I need to know what they are in

Maybe this will have you visualize. This is not all of them, but this is the pattern it is basically.

while wait(1) do
for i,ceiling in pairs(game.Workspace.Ceiling.Model:GetChildren()) do
for loop,theirchildren(ceiling:GetChildren()) do
if theirchildren:FindFirstChild("CoreLight") then
if theirchildren.CoreLight:FindFirstChild("SurfaceLight") then
for e,tofindplayers(workspace:GetChildren()) do
if tofindplayers:FindFirstChild("Humanoid") then
if (theirchildren.CoreLight.Position - tofindplayers.HumanoidRootPart.Position).Magnitude > 25 then
theirchildren.CoreLight.SurfaceLight. --whatever :D
end
end
end
end
end
end
end
end

You can reply me if you live problems or need anything :slight_smile:

Hey,

Theres a few red underlines? Where is the problem

I’m so sorry, it should be fine now

while wait(1) do
for i,ceiling in pairs(game.Workspace.Ceiling.Model:GetChildren()) do
for loop,theirchildren in pairs(ceiling:GetChildren()) do
if theirchildren:FindFirstChild("CoreLight") then
if theirchildren.CoreLight:FindFirstChild("SurfaceLight") then
for e,tofindplayers in pairs(workspace:GetChildren()) do
if tofindplayers:FindFirstChild("Humanoid") then
if (theirchildren.CoreLight.Position - tofindplayers.HumanoidRootPart.Position).Magnitude > 25 then
theirchildren.CoreLight.SurfaceLight. --whatever :D
end
end
end
end
end
end
end
end

You can reply me if you live problems or need anything :slight_smile:

Well there is no errors, but strangely nothing is occurring. This is what I got.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Name = Player.Name

local Character = game.Workspace:WaitForChild(Name)


while wait(1) do
	for i,ceiling in pairs(game.Workspace.Ceiling.Model:GetChildren()) do
		for loop,theirchildren in pairs(ceiling:GetChildren()) do
			if theirchildren:FindFirstChild("CoreLight") then
				if theirchildren.CoreLight:FindFirstChild("SurfaceLight") then
					for e,tofindplayers in pairs(workspace:GetChildren()) do
						if tofindplayers:FindFirstChild("Humanoid") then
							if (theirchildren.CoreLight.Position - tofindplayers.HumanoidRootPart.Position).Magnitude < 25 then
								theirchildren.CoreLight.SurfaceLight.Brightness = 0
								print("fixed.")
							else
								print("not fixed.")
							end
						end
					end
				end
			end
		end
	end
end

Does it print them? (character limit)

is that number 25 measured in studs?

It does not print Fixed or Not Fixed.

I don’t know how how long a magnitude is, you can make it longer

You can put print() to everywhere to see the problems, I can try it by myself but not right now :+1:

It is within a local script btw,

Nope, or only the localplayer would see if the lights were off