GetPartsInPart Doesn't detect my player

I wanted to try using Region3’s for the first time so I made a simple code that prints what it finds in the part. Although it detects singular parts it doesn’t detect my player inside of it. I looked around and even went through a whole tutorial (https://www.youtube.com/watch?v=BsC8px_oXdM) but I couldn’t find anyone else with the same problem.

local objectsInSpace = workspace:GetPartsInPart(script.Parent)
repeat
	wait(1)
	print(objectsInSpace)
until nil
1 Like

Change to

while wait(1) do
    local objectsInSpace = workspace:GetPartsInPart(script.Parent)
    print(objectsInSpace)
    -- if the above print prints an memory address e.g. 0x7ffe5367e044 try
    for i, part in ipairs(objectsInSpace) do
        print(part.Parent) -- since it detects parts of the player character
    end
end
1 Like

it doesn’t detect immediately, try

repeat
	wait(1)
	local objectsInSpace = workspace:GetPartsInPart(script.Parent)
	print(objectsInSpace)
until nil
1 Like

I know this isn’t what you’re asking for, but take a look at ZonePlus.

1 Like

By the way, just letting everyone know, you should use task.wait() instead of wait(). They are basically the same thing, except task.wait() is newer and more optimized.

3 Likes

Becouse you print already cached non edited variable?

local part = script.Parent
local GetPartsInPart = workspace.GetPartsInPart
while true do
print(GetPartsInPart(workspace,part))
task.wait(1)
end
1 Like

Yeah, this has already been fixed in 2 other posts. Thanks for the explanation, though.

Sorry for not marking the solution; ive just been out lately :sweat_smile:

1 Like

It wasnt.
Code were horrific.
My version simplifies and optimizes it.

All good! I would say this is the closest to the solution out of the posts so far.


But I don’t think anyone did a good job explaining and it still could be improved a bit, so I will try.
The problem was that you were only calling workspace:GetPartsInPart ONCE, right when your script first runs.

In your repeat loop, you are simply repeatedly printing the objects that were detected when the game started, which won’t detect any players that come into the part after the game first started. To fix this, you would put the workspace:GetPartsInPart INSIDE of the repeat block like so:

repeat
	wait(1)
    local objectsInSpace = workspace:GetPartsInPart(script.Parent)
	print(objectsInSpace)
until nil

While this is good, there are a few things we can do to make the code cleaner. First, we use a while loop instead of a repeat loop.

while wait(1) do
    local objectsInSpace = workspace:GetPartsInPart(script.Parent)
    print(objectsInSpace)
end

Cool. Now it looks so much cleaner. But one thing to note is that wait() is deprecated, meaning that you should no longer use it. Instead, use task.wait() - it’s faster/more optimized and is the new standard. Try to remember this whenever you script. With this change, the resulting code will be:

while task.wait(1) do
    local objectsInSpace = workspace:GetPartsInPart(script.Parent)
    print(objectsInSpace)
end

Detecting the player is another process. First, you must get the detected part’s parent. Then, you should check if it has a player connected to it. You can do this by using game:GetService("Players"):GetPlayerFromCharacter(). Let’s add this in:

local Players = game:GetService("Players")

while task.wait(1) do
    local objectsInSpace = workspace:GetPartsInPart(script.Parent)
    print(objectsInSpace)
    for _, part in objectsInSpace do -- simply iterating is faster than pairs or ipairs
        local character = part.Parent
        local player = Players:GetPlayerFromCharacter(character)
        if not player then continue end
        -- use player (u could also have a function that takes in the player and does wtv)
    end
end

And that’s it! I hope that helped!

Thanks! This did help. I accidently used wait this time but I swear I usually use task.wait() :folded_hands: :melting_face:. Also I think that the return end makes it end permanently which isn’t a problem for me since I already fixed it myself but I just wanted to point it out.

1 Like

Although your way was faster than pairs, this was my solution for it that personally worked for me.

local Players = game:GetService("Players")

while task.wait(1) do
	local deb = false
	local objectsInSpace = workspace:GetPartsInPart(script.Parent)
	for i, v in pairs(objectsInSpace) do
		if v:IsA("BasePart") then
			if v.Parent:FindFirstChild("Humanoid") then
				if deb == true then
					--i didnt do return end because that makes the script end permanently which i DONT want
				else
					deb = true
					local player = Players:GetPlayerFromCharacter(v.Parent)
					print(player)
				end			
			end
		end
	end
end

Sorry! I meant to use continue! Continue would only skip that iteration of the loop. I’ll edit the solution. Thanks for pointing that out.


But why use pairs? It doesn’t seem like you have a specific reason to use it in that code; it just slightly decreases performance.


Edit: Also, I highly recommend ZonePlus like I mentioned earlier - it’s super easy to use, extremely clean (in terms of organization), and very reliable.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.