Hello! I am making a game in which there are monsters that chase you. I want to have a way to escape the monsters, so they are not constantly after you. One way to get a certain monster to stop chasing you is standing under a street light, this will cause them to stop chasing you and run away. One thing I am currently struggling on is the best way to go about detecting if a player is under the light. Since the beam has no physical properties, I was instead thinking of placing a part under the street light that the player can collide with. There would be a script somewhere that would constantly check if the playing is touching a part with a certain name. However, that seems excessive, and may be heavy on performance. I am trying to find the best way to go about this, that will not require too much from the client or cause lag. There is also the possibility of raycasting although I am less comfortable with scripting that. Any input or suggestions would be awesome, thanks!
I’d create an invisible part that you can cast a ray above the character to so that if that part is hit, then you are considered under that bulb. The best part about this is that if you are not directly under the light, you are at least within the vicinity to meet the cast point of the light.
No, you would just cast a ray from the character to about as many studs upward it will be to reach a bulb, and if it does hit a part that is either named BulbVicinityPart or something of that matter, then you should do whatever you want through there. This shouldn’t require a vast amount of scripting.
I dont know if that would be so easy because it needs an event to send a message to the server that the monsters will like walk away if it hit the invisible block
You can cast the ray on the server for each character, so long as your player count does not exceed something like 20 players. Raycasting is costly to the performance of a game, so if you keep it to 20 or under, you should be fine doing it from the server.
I’ll further add that raycasting essentially not only takes that lookahead point but also requires that you multiply it by the distance in studs for it to go, so you can tell the ray that it should only cast from X-5 to X+5, which should be 10 studs and no further.
check if they are under a street light is a reasonable approach. Here’s a summary of the suggestions:
Create an invisible part (named something like “BulbVicinityPart”) under the street light.
Use a script to cast a ray from the player to the vicinity above them.
If the ray hits the “BulbVicinityPart” or any designated part, trigger the desired behavior (monsters stop chasing, etc.).
This method minimizes the need for multiple scripts or complex setups, and it should be performance-friendly. Just make sure to handle the events efficiently, especially if your game might have a higher player count.
RenderStepped is only available on the client. If you’d like to optimize for performance, you should probably cast rays from the character to the bulb from the client and then communicate with the server with a RemoteEvent so that the server understands that you want to do something.
I created the raycast in StarterCharacterScripts, but you are saying that I should cast the ray from the server instead using only one script that way it doesn’t require a remote event? Sorry I am a bit confused since I am not the best at scripting.
Below is an example script in Lua for Roblox that you can use as a starting point. This script assumes that you have an invisible part named “BulbVicinityPart” placed under each street light.
local BulbVicinityPartName = "BulbVicinityPart"
-- Function to check if the player is under the light
local function isPlayerUnderLight(player)
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoid and humanoidRootPart then
local rayStart = humanoidRootPart.Position
local rayDirection = Vector3.new(0, 1, 0) -- Upward direction
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {player.Character} -- Exclude the player's character
local rayResult = workspace:Raycast(rayStart, rayDirection, raycastParams)
if rayResult then
local hitPart = rayResult.Instance
return hitPart and hitPart.Name == BulbVicinityPartName
end
end
end
return false
end
-- Example: Check if a player is under the light
local player = game.Players.LocalPlayer -- Replace with the actual player you want to check
if isPlayerUnderLight(player) then
print("Player is under the light!")
else
print("Player is not under the light.")
end
This script defines a function isPlayerUnderLight that checks if a given player is under the light by casting a ray from the player’s position upwards and checking if it hits the “BulbVicinityPart.” You can integrate this script into your game and adapt it according to your specific requirements.