So I’m making a basketball game and I’m trying to make one script that handles all the balls. I’ve been using Collection Service and I’m stuck trying to run the code on Instanced and Non-Instanced Balls. I’ve been using for loops to run the code but while the loop is running the event I have set in place to tag instanced balls doesn’t fire.
-- Services
local CollectionService = game:GetService("CollectionService")
--Loops through workspace when game starts and adds Ball Tag to Basketballs
for _, Basketball in pairs (workspace:GetDescendants()) do
if Basketball:IsA("MeshPart") and Basketball.Name == "Basketball" then
CollectionService:AddTag(Basketball,"Ball")
print("TagAdded")
end
end
workspace.DescendantAdded:Connect(function(Descendent)
print("Descendent".."Added")
if Descendent:IsA("MeshPart") and Descendent.Name == "Basketball" then
CollectionService:AddTag(Descendent,"Ball")
print("TagAdded2")
end
end)
--^^Doesn't Fire while Loop is running
--Basketball Handler
for i,v in pairs (CollectionService:GetTagged("Ball")) do
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print("Hit")
end
end)
end