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
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)
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:
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
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
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.
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.