How to know if a player is in a part?

  1. What do you want to achieve?
    I want to know when a player is in a part .
  2. What is the issue?
    I can’t know when a player is in a part …
  3. What solutions have you tried so far?
    I’ve tried this but it doesn’t work :
workspace.Plot1BaseplateCollision.Touched:Connect(function()
	print("plauer is inside")
end)

workspace.Plot1BaseplateCollision.TouchEnded:Connect(function()
	print("plauer is outside")
end)
2 Likes

Hi, so there are two ways I know how to get if a player is in a part, Region3 and BasePart:GetTouchingParts. Lets try the second one. What you could do is Plot1BaseplateCollision:GetTouchingParts and put that in a for loop. If a part or player is inside or touching the part it will be in this table. Then check for the player so try

while wait(0.1) do -- check every 0.1 seconds
     for i, v in pairs(Plot1BaseplateCollision:GetTouchingParts) do
     
          local humanoid = v.Parent:FindFirstChild("Humanoid") -- looks for humanoid

          if humanoid then -- checks if it finds the humanoid
               print("player is inside")
          end
     end
end
2 Likes

it doesn’t work :neutral_face: :

while wait(0.1) do
	for i, v in pairs(workspace.Plot1BaseplateCollision:GetTouchingParts()) do
		local Humanoid = v.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			print("player is inside")
		end
	end
end

It’s the first time I use for loops like this, I’m a quite lost …

Do any errors print? Also, I recommend putting that inside task.spawn(function() OR spawn(function() so it doesn’t yield any other code UNLESS it’s the only thing running in the script OR you want it to yield

Yield means it won’t run anything else past the loop while the loop is running

1 Like

Do you think you could give us more details? Maybe a screenshot? Is Plot1BaseplateCollision a part?

1 Like

Use region 3, It’s like sound regions except you replace it with whatever u wanna replace it with

2 Likes

The best implementation is probably using the spatial query API which works especially well with finding parts within other parts. You can use this with the player’s character’s part:

local players = game:GetService('Players')

local playersInPartLastIteration = {} -- players who were in the part in the last iteration
local partToCheckCollision = script.Parent

while true do
	local parts = workspace:GetPartsInPart(partToCheckCollision)
	local possiblePlayers = {} -- players who are in the part
	
	for _, part in parts do
		local player = players:GetPlayerFromCharacter(part.Parent)
		if player and not table.find(possiblePlayers, player) then -- we check that their player hasn't been found in the part already, append to the table of possible players
			table.insert(possiblePlayers, player)
		end
	end
	
	for _, player in possiblePlayers do
		if not table.find(playersInPartLastIteration, player) then
			-- the player wasn't found in the last iteration so this is their first time in the part
			print('Player entered part')
		end
	end
	
	for _, player in playersInPartLastIteration do
		if not table.find(possiblePlayers, player) then
			-- player was found in last iteration but not this iteration so this is their first time leaving the part
			print('Player left part')
		end
	end
	
	task.wait(0.1) -- you don't really need to check every frame, 0.1 seconds should suffice
	-- finally, set the last iteration to be the result from this iteration
	playersInPartLastIteration = possiblePlayers
end
3 Likes

i have Good Module For You This Can Help You

this Module Can Check If Player Entered Part Or Exited From It and Can Check Any Item You Can Add it To Check if Entered or Not

Example:

local container = workspace.AModelOfPartsRepresentingTheZone
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
    print(("%s entered the zone!"):format(player.Name))
end)

zone.playerExited:Connect(function(player)
    print(("%s exited the zone!"):format(player.Name))
end)
1 Like

Thank you but I want to do it by myself ! :smile:

1 Like

This should work :

local PlayersInside = {}

Plot1BaseplateCollision.Touched:Connect(function(hit)
   if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
      table.insert(PlayersInside, hit.Parent.Name)
      print(PlayersInside[hit.Parent.Name].." just entered in the zone!")
   end
end)

Plot1BaseplateCollision.TouchEnded:Connect(function(hit)
   if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
      table.remove(PlayersInside, hit.Parent.Name)
      print("A player just left the zone!")
   end
end)
1 Like

Yes it is a part, and there is no error in the output ! :slightly_smiling_face:

2 Likes

But how do I use it for get if a player is in a part I don’t understand …

1 Like

We can’t exactly give you entire lessons here if you want to learn region3 there’s a lot of tutorials out there!

2 Likes

Did you even try my script?

eeeeeee char limit

1 Like

Yes I try it and it does not work …

1 Like

What do you mean it doesn’t work?
It isn’t supposed to work. It is supposed to show you an example of how to do it.

And we don’t even know what you want to do exactly.

1 Like

So when I went in the part that prints all the time :
Player has entered
Player has leave
Player has entered
Player has leave

So I want know by who way I can Know if a player is inside a part ! :grin:

1 Like
Plot1BaseplateCollision.Touched:Connect(function(hit)
   if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
      print(hit.Parent.Name.." just entered in the zone!")
   end
end)

Plot1BaseplateCollision.TouchEnded:Connect(function(hit)
   if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
      print(hit.Parent.Name.." just left in the zone!")
   end
end)
1 Like

Put this code in your part, and check your output.

1 Like

This script do the same has later … :neutral_face: