Im making an NPC where it shoots out bullets, but I don’t
know how to tell if the bullet has hit something. Can anybody help?
for i = 0,75 do
task.wait()
Bullet.CFrame += Bullet.CFrame.LookVector * 5
local hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector*5)
if hit and hit.Instance.Name ~= Bullet.Name then
print('Hit')
end
end
You aren’t blacklisting the bullet, which means the raycast hits the bullet, but doesn’t even print because it fails the if statement: ‘hit.Instance.Name ~= Bullet.Name’.
To blacklist, you need to add RaycastParams to your raycast:
for i = 0,75 do
task.wait()
Bullet.CFrame += Bullet.CFrame.LookVector * 5
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Bullet}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector*5,raycastParams)
if hit and hit.Instance.Name ~= Bullet.Name then
print('Hit')
end
end
EDIT:
As a side note, you should raycast from where the bullet was before moving forward, and where the bullet is currently. Otherwise, targets standing at extremely close ranges will be ignored.