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

How to detect whether a player is in a specific area every few seconds?

Asked by 3 years ago

Im trying my hand at creating doors, and ive started to make an automatic sliding door. However the problem is the whole code i have worked out in my head only works if this line of code works.

while true do 
    local TouchingParts = script.Parent:GetTouchingParts()
    print(TouchingParts)
    wait(.5)
end

And for whatever reason it does not. All I get are empty tables in the output and all im trying to do is find out whats touching the part. Im sure there is a more efficient way to do this with region3's or something, but for now I just want to see if I can get it to work, il optimize it a bit better later.

3 answers

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago

The best way is Region3 as you said.

Your method does not work because the region part your script is inside of is not collideable so :GetTouchingParts will return empty table. You could create .Touched event to create the touch interest but that's disgusting.

Regions in Roblox are quite simple, you just specify 2 vectors, TopLeft and BottomRight positions of the region. For comfort you can make the region have same size and position as specified part which will make it easier to use.

local Part = script.Parent

while true do

    local Region = Region3.new(

        Part.Position - (0.5 * Part.Size),
        Part.Position + (0.5 * Part.Size)
    )

    local TouchingParts = workspace:FindPartsInRegion3(Region, Part, math.huge)

    print(TouchingParts)

    wait(0.5)
end

The region was created from position and half size of the part? I don't know that math is too complicated for me however it works, just remember that.

:FindPartsInRegion3 is function of workspace which returns table of all parts inside of given region, 2 argument is filter, in this case it will filter Part and all of it's descendants so they won't appear in the table, it can be nil but it's up to you, the third argument is max amount of parts you want to be in the region, i used the hugest number possible but it's up to you again.

Region3

FindPartsInRegion3

0
I have just one question, is there a way to add a region3 before running the game, or do I need to create one using a script? generalYURASKO 144 — 3y
0
No, the only possible way would be is to create plugin which stores CFrame and Size of the region that you want to create and then use it to create the region, however, not sure if that is usefull. imKirda 4491 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Try adding a part that the player will touch to activate the door and add a script to that and put this:

function onTouch()
--Your Script
end
script.Parent.onTouched:connect(onTouch)

This should work :3

0
He is trying to wrap that in a while loop so I don't recommend using functions. Kykysurvivin 20 — 3y
Log in to vote
0
Answered by 3 years ago

Try using a proximity part that when the player hits it fire's something. If this isn't what you would like then you could comment, but I took it that you wanted an automatically opening door. this is what I found worked for me local Touched = false You could remove the print local Touched = false while wait(.5) do -- your time here local ProxPart = script.Parent.ProxPart -- Make a proximityPart and have the name here ProxPart.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and Touched == false then -- checks if hit is a player object Touched = true print("Prox Touched") -- your code here wait(3) Touched = false -- makes it so the player can't glitch the door end end) end

Answer this question