I’m trying to make a system where an enemy launches a projectile at you that damages you.
The launching part works perfectly fine, however the raycast it fires never detects any instances, let alone players.
This is the script that I am using:
local closestPlayer = findClosestPlayer(sock)
if closestPlayer then
local roach = game.ReplicatedStorage.Roach:Clone() -- roach is the projectile
roach.Parent = sock -- sock is the enemy
roach.Position = sock.PrimaryPart.Position
roach.CFrame = CFrame.lookAt(roach.Position, closestPlayer.Character.Head.Position)
roach.CanCollide = false
game:GetService("Debris"):AddItem(roach, 10)
local params = RaycastParams.new()
params:AddToFilter(roach)
params:AddToFilter(sock)
params:AddToFilter(workspace.Socks)
for i, v in pairs(workspace:GetDescendants()) do if v:IsA("Accessory") then params:AddToFilter(v) end end
params.FilterType = Enum.RaycastFilterType.Exclude
delay(0, function()
while roach do
task.wait()
roach.CFrame += roach.CFrame.LookVector * 1
end
end)
local hit = workspace:Raycast(roach.Position, roach.CFrame.LookVector * 5, params)
if hit then print(hit.Instance) end -- never prints anything
if hit and game.Players:GetPlayerFromCharacter(hit.Instance.Parent) then
print("hit")
closestPlayer.Character.Humanoid:TakeDamage(3 + (game.ReplicatedStorage.Room.Value / 400))
roach:Destroy()
end
end
What essentially happens is that the projectile passes right through the player, as well as any other part.
Honestly I don’t know why this is the case, as the params seem to be in order.
Any help is appreciated. Many Thanks.