Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detecting a player in Region3?

CODE:



region = Region3.new(Vector3.new(-13.375, 1.85, 6.58),Vector3.new(7.63, 3.7, 5.12))

while true do
	wait()
	for i,v in pairs(game.Workspace:FindPartsInRegion3(region,nil,1000)) do
		if v:FindFirstChild("Humanoid") then
			---do crap here
		end
	end
end

I'm having trouble finding what I need. ALL I wanna do is if a player is in this region, do this. AND if a player leaves, stop this.

2 comments

0

^ Obviously the code doesn

0

n't work*

1 answer

✓ Accepted answer

To find a player, you need to use :GetPlayerFromCharacter(), because :FindPartsInRegion3() will be detecting a part, which will be a child of the player's character.

In addition to your code, then...

region = Region3.new(Vector3.new(-13.375, 1.85, 6.58),Vector3.new(7.63, 3.7, 5.12))

while true do
	wait(.1) --you probably don't need a loop iterating so quickly
	for i,v in pairs(game.Workspace:FindPartsInRegion3(region,nil,1000)) do
		if v.Parent:FindFirstChild("Humanoid") then --determine if it's player-like
			local Player = game:GetService("Players"):GetPlayerFromCharacter(v.Parent)
			if Player then --confirm it is a real player
				--do code
			else
				--do code for player leaving
			end
		end
	end
end

3 comments

1

thank you this worked. How would I make it so when the player leaves the region, whatever happens?

0

Well, it is wrapped in an infinite loop. Thus, you can just add an else to the if statement. See my edited code.

0

Accept and upvote if this helps

This discussion is preserved as it was. New answers, comments, and votes are closed.