Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[Region3] Detect if player is Region3?

Asked by
NorteX_tv 101
5 years ago
Edited 5 years ago

I need to detect if LocalPlayer is in Region3

local plr = game.Players.LocalPlayer
while 1 do
    if(plr.IsInRegion3 == "RegionName") then --this is obviously wrong, I need a solution for this
        --Do something
    wait(0.1)
end
0
Why is 1 your condition in a while loop? User#24403 69 — 5y
0
Because I want to, why not. NorteX_tv 101 — 5y
0
Also, that doesn't bring anything to answer. NorteX_tv 101 — 5y
0
Because it doesn't make any sense to use a number as a condition. Opt for readability. Can you explain why it works? User#24403 69 — 5y
0
1 as the condition is just weird User#23365 30 — 5y

1 answer

Log in to vote
1
Answered by
ozzyDrive 670 Moderation Voter
5 years ago

You need to utilize the workspace.FindPartsInRegion3WithWhiteList function. You can call the function with the character model in the whitelist every frame or two, that's up to you to decide. Calling the function with only a whitelist that allows ~20 parts doesn't make it too expensive to run every frame. Of course if you don't need to call it that often, it's simple to optimize. Keep track of the last update and see if enough time has passed since.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local region = Region3.new(Vector3.new(-10, -10, -10), Vector3.new(10, 10, 10))

--

local function isPlayerInRegion(player, region)
    local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, {player.Character})
    return #partsInRegion >= 1
end

local function regionUpdate()
    if localPlayer.Character then
        local localPlayerIsInRegion = isPlayerInRegion(localPlayer, region)
        localPlayer.Character.Head.BrickColor = localPlayerIsInRegion and BrickColor.Green() or BrickColor.Red()
    end
end

--

RunService.Heartbeat:Connect(regionUpdate)  --  No reason to do it before the frame is drawn by binding to the RenderStepped event, using Heartbeat here is the best
0
I tried it, and it doesn't work, it turns the head red everytime, even if I'm not in region. NorteX_tv 101 — 5y
0
(doesn't throw any errors tho) NorteX_tv 101 — 5y
0
Are you sure you ever actually were in the region? The sample's region is a 10x10x10 cube, positioned at (0, 0, 0). It does work fine for me. ozzyDrive 670 — 5y
0
I made sure it's good, I even tried with showing part of region size and it showed it. As I thought about it rn, I've seen that even when the player is in the region it still prints 0 parts. NorteX_tv 101 — 5y
Ad

Answer this question