So I am making an archer that shoots arrows but one thing that doesn’t really work is GetTouchingParts.
It doesn’t detect the touched even though the projectile got touched.
local function GetTouchingParts(part)
if part ~= nil then
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts() -- not really working
for i,v in pairs(results) do
connection:Disconnect()
return v
end
end
end
Remote.Event:Connect(function(firePoint)
castParams.FilterDescendantsInstances = {character, HeroAbilitiesFolder}
local projectile = Arrow:Clone()
projectile:PivotTo(CFrame.new(x0))
projectile.Parent = HeroAbilitiesFolder
local nt = 0
RunService.Heartbeat:Connect(function(stepped)
nt += stepped
-- fire arrow
local results = GetTouchingParts(projectile.PrimaryPart)
if results then
hitDetection(results, projectile)
end
end)
end)
local function GetTouchingParts(part)
if part ~= nil then
local results = part:GetTouchingParts()
for i,v in pairs(results) do
return v
end
end
end
RemotesFolder.FireProjectile.Event:Connect(function(firePoint, hitPosition)
castParams.FilterDescendantsInstances = {character, HeroAbilitiesFolder}
local projectile = ToolData.Projectile:Clone()
projectile:PivotTo(CFrame.new(x0))
projectile.Parent = HeroAbilitiesFolder
local nt = 0
coroutine.wrap(function()
local connection = RunService.Heartbeat:Connect(function(stepped)
nt += stepped
if projectile.PrimaryPart ~= nil then
local touchedConnection = projectile.PrimaryPart.Touched:Connect(function() end)
local results = GetTouchingParts(projectile.PrimaryPart)
if results then
hitDetection(results, projectile)
touchedConnection:Disconnect()
end
end
end)
table.insert(connections, connection)
end)()
Derbis:AddItem(projectile, ToolData.ProjectileDuration)
end)
Yes and you also need the connection outside the function. Try this…
"
local function GetTouchingParts(part)
if part ~= nil then
local results = part:GetTouchingParts()
for i,v in pairs(results) do
return v
end
end
end
local projectile = ToolData.Projectile:Clone()
projectile:PivotTo(CFrame.new(x0))
projectile.Parent = HeroAbilitiesFolder
local nt = 0
coroutine.wrap(function()
local connection = RunService.Heartbeat:Connect(function(stepped)
nt += stepped
if projectile.PrimaryPart ~= nil then
local touchedConnection = projectile.PrimaryPart.Touched:Connect(function() end)
local results = GetTouchingParts(projectile.PrimaryPart)
if results then
hitDetection(results, projectile)
touchedConnection:Disconnect()
end
end
end)
table.insert(connections, connection)
end)()
Derbis:AddItem(projectile, ToolData.ProjectileDuration)
end)
local connection = part.Touched:Connect(function() end)
To expand, yes you need the Touched connection for GetTouchingParts to work, but the connection must be established before touches occur. It is best to create the connection at the start of the projectile’s lifetime like so, and it will be cleaned up when the projectile is destroyed.
local projectile = Arrow:Clone()
projectile:PivotTo(CFrame.new(x0))
local connection = projectile.PrimaryPart.Touched:Connect(function() end)
projectile.Parent = HeroAbilitiesFolder
But why aren’t you using the Touched connection directly? It looks like your code is inefficiently re-creating the Touched signal. This edit using the connection directly should work the same if not better and removes a stuttering while loop