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
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
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
I tried it, and it doesn't work, it turns the head red everytime, even if I'm not in region.
(doesn't throw any errors tho)
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.
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.
This discussion is preserved as it was. New answers, comments, and votes are closed.
5 comments
Why is 1 your condition in a while loop?
Because I want to, why not.
Also, that doesn't bring anything to answer.
Because it doesn't make any sense to use a number as a condition. Opt for readability. Can you explain why it works?
1 as the condition is just weird