Detecting Player In a Certain Area Question

What would be the best way to make a system where when a player enters an area, it is detected - and you can run some code after that to make something happen?

Thanks!

You can use touch events to detect a player in certain area.

1 Like

i think touch event is verry lag

1 Like

Is this reliable though? I heard it wasn’t the best way to go about it.

-- Put this local script in StarterCharacter   and read my recomend in the script
local char = script.Parent
    local hrp = char:WaitForChild("HumanoidRootPart")
    local areasGroup = workspace:WaitForChild("Part_En")
    local currentArea = nil
    game:GetService("RunService").Heartbeat:Connect(function()
    	local raycast = Ray.new(hrp.Position, hrp.CFrame.UpVector * -1000)	
    	local part = workspace:FindPartOnRayWithWhitelist(raycast, {areasGroup})
    	if part and part.Parent == areasGroup then	
    		if part ~= currentArea then	
    			currentArea = part
    			
    -- You wwill know whwen a plr enter arena in here. U must make a part in workspace under arena called "Part_en".This script by HowToRoblox
    		end	
    	else		
    		currentArea = nil
    	end
    end)
1 Like

Apart from plugins you’ve probably already heard about (ZonePlus v3.2.0 | Construct dynamic zones and effectively determine players and parts within their boundaries).
There are a lot of different possible options for area detection:

One: Humanoid.FloorMaterial

Using humanoid.floormaterial, you could get the material the player is standing on every time the material of the floor changes. For example, if the humanoid steps onto a part which has the grass material, play grass stepping noises.

Two: Raycasting

You could cast a ray downwards from the player humanoid root part after a set amount of time (or every frame using BindToRenderStep) and run some code if say, the RayCastResult.Material was grass.

Edit: I wouldn’t suggest using touched functions as the result tends to be inaccurate. On the other hand, if you are going to use raycasting, you should use the new raycasting methods, as ray.new() has been deprecated.

6 Likes

What about region 3? Would that be a good, reliable way to do it?


Taken from zone v1 plugin, detailing some of the solutions for making zones, and their downsides

image
Region3 is fine, but what if you want a zone that isn’t just a cube?

2 Likes

Well, do they have to be exact cubes?

Could you elaborate on what you’re planning on doing with region3’s?

1 Like

Ok, so in all the different region 3 areas there will be crystals. When a player walks inside a region 3 area, I want the player’s Found Crystals GUI to update with a new GUI image that displays that new crystal they just “found”.

In that case region3 or magnitude would work perfectly fine.
If you don’t want the area to be cubed, you could use magnitude checks to find the distance between two points.

1 Like

@octavodad Region3 can be costy if used a lot. I recommend using Raycasting, if you’re making a region script where you want it to detect what area a player is in then I suggest adding bricks in the sky and just having a ray shoot upwards. If it detects a brick, it checks the name etc etc.

The reason I think this would be the best method is because if for instance you have a few small spots of your area that are unfilled you can just add in some extra bricks there with the same name which your script would recognize and it wouldn’t affect anything.

2 Likes

if the player is standing on a ledge with one leg over the ground and one in the air, the ray will miss as it raycasts down from the center of the character.

it will return nil or nothing idk if the player jumps
however both the errors i stated can be fixed.

1 Like

This question is linked to this question. If you know how to do this, please tell me. How To Find The Name Of This?