Detecting on which map the character is?

I want to detect on which map the character is, however, I am not sure which method to use.

I can either do:

  • Touched event in the parts which the characters are expected to walk on.
  • Raycasting to detect the part the character is walking on.
  • Loop which gets Region3 for every part
  • Loop which gets the nearest base

I’m not really sure which method is the least expensive for all platforms, unless there is any other method.

What are your thoughts?

1 Like

I have seen your game, and you have these walls which players go through in order to progress.
To do this, when a player joins you can set a default value. For example; if a player just joins the game and your first level is where they spawn (duh), you set a variable to ‘1’.

Each time a player goes through a wall (You detect this using a collision handler which can be done by many things, .Touched is one of them), you check if the value you assigned is greater than the one assigned to that wall, and if the condition returns ‘true’ then your player has walked back to a previous level, but if it returns ‘false’, the player has advanced to another level. Each time this happens, update the player’s value to the respective level value.

If you have a teleportation feature in your game, say, to sell things; you can just set the variable to the default value (eg. 1).

An event would probably cause the least lag, but a loop that runs on the server (and uses RemoteEvents, if needed) shouldn’t be an issue. FindPartsInRegion3WithWhiteList is probably most efficient with this and very easy to set up, however with the limitation that the Regions used have to be axis-aligned and can not be rotated or differ from a cuboid in any way.

1 Like

Need to keep in consideration that I would need a loop to check the Region3 for every map. Not sure how expensive that may be.

Couldn’t you have Region3 “Borders” around the edges of each map, as well as Touched Events near all Spawn/Teleportation Points? It definitely won’t be as costly as scanning the entire area for the player’s presence.

Yeah, but if I did the check every 5 seconds and have 10 maps, there would be 10 Region3 checks every 5 seconds. Sounds a bit expensive to me but I am not sure. If I had to make use of any Touched Events, then I might as well not need the Region3, however, what I am afraid of is the number of times the event will be unnecessarily triggered.

I can guarantee you Region3s are most efficient and least costly, but I’m just not sure how costly it is in your scenario. As you said, touched events are very unpredictable, and sometimes rare cases occur. For example, a group of players piling up on a spawn means that not everyone would touch the spawn.

Figured out the best idea is to get the nearest Position of the HumanoidRootPart of characters in respective to the map’s bases’ positions.

3 Likes

The best idea is use a while true do with a wait that you can choose how many seconds the game will detect if the player is on another map, then you can inside the loop make a region 3 arround the humanoidroot part, and the region3 could be small
then using this:
https://developer.roblox.com/api-reference/function/Workspace/FindPartsInRegion3WithWhiteList

you can just whitelist a folder that will contains invisible and big parts for everymap, they should be invisible and can collide = false
then you just add the folder to the region3 and the parts that will find the script are only those blocks.

As the scripts or tools that use the player:GetMouse() to detect parts or whatever, you can just add this: Mouse.TargetFilter = folder_where_are_the_blocks and the mouse will ignore them.

More info:
https://developer.roblox.com/api-reference/property/Mouse/TargetFilter

then with this know you have just one region3 looping every x seconds only to know where the humanoidrootpart is instead of too many touch parts or too many region3 for every part.

1 Like

Although it makes a lot of sense because with your method there won’t be any unnecessary Region3’s created, I will still stick to the method I have marked as a solution for the reason being that it doesn’t involve any Region3 checks. However I very much appreciate your response.

but using the nearest position of the humanoid isn’t the best, and the region3 can work with local scripts well and doesn’t do a region3 for every part, just a region 3, and the nearest position will involve using maybe a .magnitude check for every part.

1 Like

Forgot to say but I needed the server to know where the characters are at so I had to use the server side. What I did was a nearest number check for the Z axis of the HumanoidRootPart and the bases of the maps because my maps are all on the Z axis. The method is working perfectly fine.

1 Like

I personally use a math formula to determine this. It works with any funny angles your map may be at! Here’s roughly the checks I do:

  • Get a Hitbox of the map, If you don’t have one use Model:GetBoundingBox()
  • Get the HumanoidRootPart of the player, and ensure there is a HumanoidRootPart
  • Get the Local Difference by using the formula (Hitbox.CFrame - Hitbox.CFrame.p):inverse() * (HumanoidRootPart.Position - Hitbox.Position)
  • Check that the player is inside the Hitbox by doing checks on the X and Z axis of the Local Difference by doing math.abs(LocalDifference.X) < Hitbox.Size.X / 2

If both of those checks return true, the player is inside the box, otherwise they aren’t!

I run this in a Heartbeat loop.

4 Likes