Alright, I have recently been making a mechanic in my game where multiple AI shoot arrows at other AI. Lately I’ve been noticing that sometimes the AI getting shot at doesn’t take damage but I’ve just centered the issue and do not have a solution.
Basically, here’s what’s happening. As the player moves further away from the AI getting attacked, they seem to take less damage. This is NOT an intended feature in my game and I think it might have something to do with network ownership. Here’s an example:
If anyone could help me solve this issue it would be greatly appreciated!
Note: This is not just a stroke of luck that they didn’t take as much damage. I’ve tested it multiple times
function RangedFire(RandomTarget,SpecifiedUnit,TargetGroup)
spawn(function()
SecondLock = false
if RandomTarget:FindFirstChild("HumanoidRootPart") then
local ArrowRay = Ray.new(SpecifiedUnit.Head.Position, (RandomTarget.HumanoidRootPart.Position - SpecifiedUnit.Head.Position).Unit * 1000)
local hit, position = workspace:FindPartOnRayWithIgnoreList(ArrowRay,{SpecifiedUnit})
local t
local Offset
local SpreadFactor1 = UnitData[SpecifiedUnit.Properties.UnitType.Value]["SpreadFactor1"] + math.ceil(((RandomTarget.HumanoidRootPart.Position - SpecifiedUnit.HumanoidRootPart.Position).Magnitude)/50)
local SpreadFactor2 = UnitData[SpecifiedUnit.Properties.UnitType.Value]["SpreadFactor2"] + math.ceil(((RandomTarget.HumanoidRootPart.Position - SpecifiedUnit.HumanoidRootPart.Position).Magnitude)/10)
if hit then
if hit.Parent:FindFirstChild("HumanoidRootPart") then
if hit:IsDescendantOf(TargetGroup) then
t = 1
Offset = Vector3.new(math.random(-1 * SpreadFactor1,SpreadFactor1),0,math.random(-1 * SpreadFactor1,SpreadFactor1))
else
t = 2
Offset = Vector3.new(math.random(-1 * SpreadFactor2,SpreadFactor2),0,math.random(-1 * SpreadFactor2,SpreadFactor2))
end
else
t = 2
Offset = Vector3.new(math.random(-1 * SpreadFactor2,SpreadFactor2),0,math.random(-1 * SpreadFactor2,SpreadFactor2))
end
end
local hrp = SpecifiedUnit.HumanoidRootPart
local Arrow = game.ReplicatedStorage.Objects.ProjectileArrow
local g = Vector3.new(0, -game.Workspace.Gravity, 0);
local x0 = hrp.CFrame * Vector3.new(0, 2, -2)
local v0 = ((RandomTarget.HumanoidRootPart.Position + Offset) - x0 - 0.5*g*t*t)/t;
local c = Arrow:Clone();
c.Velocity = v0;
c.CFrame = CFrame.new(x0);
c.Parent = game.Workspace;
wait(1)
c.Touched:Connect(function(Hit)
if SecondLock == false then
if Hit.Material == Enum.Material.Metal then
spawn(function()
local RandomTang = script.Parent.Parent.Parent.Sounds:FindFirstChild("Metal Tang "..math.random(1,2)):Clone()
RandomTang.Parent = Hit
RandomTang:Play()
Debris:AddItem(RandomTang,10)
c.Anchored = true
Debris:AddItem(c,2)
SecondLock = true
end)
end
if not Hit:IsDescendantOf(SpecifiedUnit) then
if Hit.Parent then
if Hit.Parent.Parent then
if Hit.Parent:FindFirstChild("Humanoid") then
local Damage = CalculateDamage(SpecifiedUnit,Hit.Parent)
Hit.Parent.Humanoid:TakeDamage(Damage)
end
if Hit.Parent.Parent:FindFirstChild("Humanoid") then
local Damage = CalculateDamage(SpecifiedUnit,Hit.Parent.Parent)
Hit.Parent.Parent.Humanoid:TakeDamage(Damage)
end
c.CanTouch = false
Debris:AddItem(c,2)
end
end
end
end
end)
end
end)
end
You likely just need to add a debounce (cooldown) on the .Touched event itself, I assume the reason for more damage occurring when closer is because the projectile has a life span and if its distance to the target is shorter then it’ll hit the target faster thus resulting in it touching that target for longer until it expires, whereas when the projectile is airborne for longer it’ll be touching its intended target for a shorter period of time thus triggering the .Touched event fewer times resulting in less damage being dealt.
The distance to the target did not change at all though? The only thing affecting it was me walking near it, Also, my debounces are called “locks”. The debounce is defined as “SecondLock” in this case