How can i make this if the Character's Torso is close to the door the PointLight turns on and when the Character's Torso is farther than 15 then it turns off. The problem is that it doesn't turn on...
local Door = script.Parent while true do for i,v in pairs(game.Players:GetChildren()) do if (v.Character.Torso.Position - Door.Position).magnitude <= 15 then Door.PointLight.Enabled = true if (v.Character.Torso.Position - Door.Position).magnitude <= -15 then Door.PointLight.Enabled = false end end end wait() end
local Door = script.Parent while true do for i,v in pairs(game.Players:GetChildren()) do if (v.Character.Torso.Position - Door.Position).magnitude <= 15 then Door.PointLight.Enabled = true elseif (v.Character.Torso.Position - Door.Position).magnitude >= 15 then Door.PointLight.Enabled = false end end wait() end
This is a working version of your script I just changed the magnitude to greater than 15 instead of less than -15 and I changed your if to an elseif statement.
Logic
There's two fundamental issues here. Magnitude will never be less than 0, and your if statements are nested with mutually exclusive conditions.
The solution is to use an else
statement, which is the compliment of the if statements preceding it. In other words, if your condition isn't true then it goes to the else block.
local Door = script.Parent while wait() do for i,v in pairs(game.Players:GetChildren()) do if (v.Character.Torso.Position - Door.Position).magnitude <= 15 then Door.PointLight.Enabled = true break -- We don't actually want it turning off because nobody else is near (Do you?) else Door.PointLight.Enabled = false end end end
A quick note to you though - This will break when a new Player tries to join, as the Player exists before the Character on a real server.