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
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.
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.
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
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
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