I am trying to make a magic spell attack by creating a new Instance and using GetTouchingParts and printing every part from the GetTouchingParts table, but when the spell is activated only the tool will get printed out. I randomly tried to put a Touched event in front of the table as shown bellow and the script worked perfectly, the everything I needed to print out got printed and no lag occurred. Why is this working when I put a touched event in front?
local sphere = Instance.new("Part")
sphere.Parent = workspace
sphere.CFrame = tool.Handle.CFrame
sphere.Size = Vector3.new(7,7,7)
sphere.Anchored = true
sphere.CanColide = false
event:FireClient(plr, "attack"..nr.." is here!")
wait(.1)
sphere.Touched:connect(function() end) --if i remove this line it will print the hhandle -WHY??? IDK!!!!
local table2 = sphere:GetTouchingParts()
for _, hit in pairs(table2) do
if not hit.Parent == character then
print(hit.Name.." "..hit.Parent.Name)
end
end
wait(.75)
shpere:Destroy()
I tested it with the “sphere.Touched” line and without a lot of times. Why is this happening, does the touched event stop the script for some nano-seconds or what?
GetTouchingParts doesn’t work on parts with CanCollide set to false. By connecting to Touched you essentially “wake” it up in terms of collision detection. Not sure where the tool handle is coming from though.
A little off-topic, but seeing that you’re creating magic spells, I feel I had to point it out - you should move the Parent set to the last line below the CanCollide statement (which is spelled wrong, by the way).
Here’s a related thread in regards to parenting that you can read up on, that contains information regarding setting parents first before properties: