How to check if the player is in light?

Hey there, my name is ShieldDevs. I’m a scripter and a game developer.

So I’ve been working on my game “Escape the Unknown”, which is a scary game. And I wanted to make it so when the player is in light, the flashlight will turn off, and if the player is in a dark place, or not close to light, then the flashlight will turn on. So how can I achieve this?

I would really appreciate it if you can help me!

Thanks,
ShieldDevs

2 Likes

I don’t believe there is anyway to use lighting and touch events together.
But you could try creating a hidden part that “when touched” and its associated light is on, could achieve a similar goal. Perhaps attempt to shape the part you use so it matches the light dispersion shape.

2 Likes

Thank you for you reply, I thought about that, but the thing is, I don’t know a “perfect” way to check if the player isn’t touching that part. Touch ended is a really bad way to do it, and Magnitude isn’t the best as well.

1 Like

In my experience. TouchEnded works best if you envelope the player inside the object first. (inside the part == touching, outside the part touch end. Otherwise you’ll probably have to rely on a pattern of raycasts. Oh you can also double check “touchended” by using “GetTouchingParts” when touchended fires. If your still touching the part then debounce it.

1 Like

You can try creating a zone using region3, if the player is inside of the zone the flashlight will be turned on/off

1 Like

there is more than 1 way.
for example region3 or Ray Casting.

But ray casting won’t do the job because the light covers some volume and not just somewhere specific.
Thus create Region3 (s) and check if the player is in that region.

1 Like

Region3 is basically the same thing as creating a part and using that. Only creating a part is much easier.

1 Like

You can add a part to every light source and use magnitude to check if a player is near a light.

if (HumanoidRootPart.magnitude - LightPart.Position).magnitude <= amount then
     -- do stuff
end
2 Likes

There is no point in connecting and firing an event the part is touched.
If part could be a replacement of Region3 I do not think roblox would have added a separate class.

2 Likes

The only problem with that is there still needs to be a way to activate that check, else you have to continuously poll it.

I don’t understand what you’re trying to say, could you elaborate?

Yeah I tried that in many different situations, but it seems like magnitude doesn’t do the job for touching and non touching events

1 Like

Region3 and distance checking do not provide a method of activation where touched does. So you must continously check to see if a player is within the region or what the distance is. That requires a while loop or heartbeat check all the time for every light source.

2 Likes

if you want it to be inaccurate/isnt core to the game just get the corners of the player and save those points then create a ray cast with those points to the light sources in your game and if any of them are true they should be in light. A much easier way would be if your game has like spot lights and they are all a constant range just check how far your player is from each light source

1 Like

Check the character’s humanoid root part vs Light.Parent.Position magnitude and then check the range of the light with (rootpart.Position - Light.Parent.Position).Magnitude < Light.Range, you can add all the lights into a table and loop through that table to have it work, the other way around would be to use GetPartsBoundInRadius(Light.Parent.Position,Light.Range,params)

2 Likes