Problem with GetPartsInPart

  1. What do you want to achieve?
    Im trying to make a script which detects if parts are within a taker’s hitbox, then perform an action to check for each part in that hitbox

  2. What is the issue?
    The problem is while both “HI” and “???” print, “SOMETHING” doesn’t print. The script doesn’t throw any errors, and I don’t get how the table passes checking if its nil but then never triggers the next print even if by this logic the table shouldn’t be empty.
    (if this isn’t the right way to check if a table is empty let me know)

  1. What solutions have you tried so far?
    I’ve tried GetTouchingParts as well and both haven’t been able to trigger the “SOMETHING” print seen above

code as a lua block:

local CollectionService = game:GetService("CollectionService")
local ScoreValue = 0
local PlacedItems = workspace:WaitForChild("PlacedItems")

PlacedItems.ChildAdded:Connect(function()
	for i, taker in  CollectionService:GetTagged("Taker") do
		-- sensing for parts in taker
		local Hitbox = taker:FindFirstChild("Hitbox")
		local TouchingParts = workspace:GetPartsInPart(Hitbox)
		print("HI")
		if TouchingParts == nil then return end
		print("???")
		-- get requirements for item to be taken
		local Requirements = taker:FindFirstChildWhichIsA("StringValue")
		for i, part in (TouchingParts) do
			print("SOMETHING")
			--check if the part matches these arguments
			if CollectionService:HasTag(part, "NotActive") == true and part:FindFirstChildWhichIsA("StringValue").Value == Requirements.Value and part:FindFirstChildWhichIsA("StringValue").Name == Requirements.Name then
				CollectionService:RemoveTag(part, "NotActive")
				-- Score!
				local ScoreEvent = game.ReplicatedStorage.Events.ScoreIncrease
				ScoreValue+=1
				ScoreEvent:FireAllClients(ScoreValue)
				-- Cleanup
				part:Destroy()
			end
		end 
	end
end)

You can also tell me whether or not the problem originates from my code or if I should check workspace itself again too!
thanks in advance!

TouchingParts is an array so you should do if #TouchingParts > 0 then. You can also print #TouchingParts to see how many parts were returned in the array.

thanks for the info!
it turns out that #TouchingParts prints 0 no matter if parts are in the hitbox or not
for extra info heres a photo of the hierarchy of the objects in workspace
the object tagged taker is in blue:

image

is CanQuery set on or off?
Sometimes if there are separate collision groups this kind of stuff can happen also.

CanQuery is set to true
There are also no collision groups in the game

Can you give us a gyazo of you running the game, so we can see the items intersecting with eachother at the point the function is run?

Thanks

I tried to reccord it in OBS
the green semi-transparent part is the hitbox
the part that im placing down gets put as a child into PlacedItems too, which is the event that fires the code that i sent in the original post

(if the video doesnt work let me know I dont normally upload videos on devforum)

Is the script that is placing the item a Server script or local?

Its also a serverscript: a client sends info that a serverscript then receives and places the part

The part that it is supposed to be finding exists, is that on the server or client only?

That is also on the server as well

In the video, is the taker the part that you are placing? Nothing is overlapping the volume so thats why its 0.

The taker’s hitbox is that small slightly transparent green part thats on top of the green part
heres a better photo since the video had a really low resolution

image

Ohhh I see it. If #TouchingParts == 0 then the second loop wont run anyway, so you dont need to return the iteration and end the loop. How many takers are there, because it’s possible that the first taker didn’t have any parts, but when it returns, it ends the loop and the second taker couldn’t check.
Return will not end the iteration, but the entire function is what I’m trying to say.

There is currently one taker, and changing the code didn’t fix the problem, but thank you since this problem couldve tripped me up in the future

edit: this made the script error, so the end stays (its required to end the then after the if)

Hmm… I did some more testing and it seems like adding a task.wait() of 1 before the actual code fixed it! I think roblox loads collisions a bit later then when the actual item gets parented into workspace, and thats what was preventing the code from working.

2 Likes
PlacedItems.ChildAdded:Connect(function()

The ChildAdded event/signal receives the child object that was added, did you want to tag this child?

PlaceItems.ChildAdded:Connect(function(Child)
--Tag child before loop.
2 Likes