Light shadow script

Hello, i want to make pointlight shadows disappear when i walk away from them, i have a working script, but a problem here that i will need copy this script for every pointlight i will make, would be too large script, so, i want to make a script that will get every point light in workspace and when i walk away from it it will disappear.

I already tried few ways, but i didn’t work for me, because me and scripting is diffrent things, can you help me out?

while wait() do
	local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - workspace.Part.Position).Magnitude
	if magnitude > 20 then workspace.Part.PointLight.Shadows = false
	else workspace.Part.PointLight.Shadows = true
	end
end

as you can see here it will make pointlight shadows dissapear, but only in one part, when i duplicate part, same name, same everything, it doesn’t work :slightly_frowning_face:

How can i achieve that?

We’ll make a loop for every point light in the workspace then basically just do what you were doing before on every light :

for _, light in pairs(game.Workspace:GetDescendants()) do
    if not light:IsA("PointLight") then continue end
    local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - light.Parent.Position).Magnitude
	if magnitude > 20 then light.Shadows = false
	else light.Shadows = true end
end

My pointlights now don’t give shadows at all when i get close, or far away :slightly_frowning_face:

Oh my bad you need to wrap this in a loop then it should work

game.RunService.RenderStepped:Connect(function()
    for _, light in pairs(game.Workspace:GetDescendants()) do
        if not light:IsA("PointLight") then continue end
        local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - light.Parent.Position).Magnitude
	    if magnitude > 20 then light.Shadows = false
	    else light.Shadows = true end
    end
end)

“Runservice is not a valid member of DataModel “Classic Baseplate””


game:GetService("RunService").RenderStepped:Connect(function()
    for _, light in pairs(game.Workspace:GetDescendants()) do
        if not light:IsA("PointLight") then continue end
        local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - light.Parent.Position).Magnitude
	    if magnitude > 20 then light.Shadows = false
	    else light.Shadows = true end
    end
end)

1 Like

Oh my god, thanks! Now it works :happy3:

1 Like

The reason the first version didn’t work was because it only checked one time and when you got close it never updated again! Glad it works now make sure to mark solution!

Thanks, i marked it! (1111111)

Oh, and also, i’m trying to make it work for surfacelight too, but it doesn’t


game:GetService("RunService").RenderStepped:Connect(function()
	for _, light in pairs(game.Workspace:GetDescendants()) do
		if not light:IsA("PointLight") or light:IsA("SurfaceLight") then continue end
		local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - light.Parent.Position).Magnitude
		if magnitude > 20 then light.Shadows = false
		else light.Shadows = true end
	end
end)

Where i made mistake?

You have to add a not before the (it should also be and not or btw)

So like this:


game:GetService("RunService").RenderStepped:Connect(function()
	for _, light in pairs(game.Workspace:GetDescendants()) do
		if not light:IsA("PointLight") and not light:IsA("SurfaceLight") then continue end
		local magnitude = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - light.Parent.Position).Magnitude
		if magnitude > 20 then light.Shadows = false
		else light.Shadows = true end
	end
end)

1 Like

bro, thank you so much! :smiley: (111111)

1 Like

No problem, glad I could help and good luck with your game

1 Like