Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my bullet touched event only detecting my player and tool?

Asked by 4 years ago
game.ReplicatedStorage.RayCast.OnServerEvent:Connect(function(player,exit,mousehit)
    local debounce = false
    local ray = Ray.new(exit.Position, (mousehit.Position - exit.CFrame.Position).unit * 500)
    local part,position = workspace:FindPartOnRay(ray,player.Character,false,false)
    local bullet = Instance.new("Part",workspace)
    bullet.Material = Enum.Material.Neon
    bullet.BrickColor = BrickColor.new("Bright yellow")
    bullet.CanCollide = false
    bullet.Anchored = true
    bullet.Name = "Ak-47 Bullet"
    local distance = (exit.Position - position).magnitude
    bullet.Size = Vector3.new(-8.855, 0.025,distance)
    bullet.CFrame = CFrame.new(exit.Position, position) * CFrame.Angles(math.rad(math.random(0,5)),math.rad(math.random(0,9)),0) * CFrame.new(0,0,-distance / 2)
    bullet.Touched:Connect(function(hit)
        print(hit)
    end)
end)

The Touched event does not detect the dummy or baseplate, it only detects the children of the tool and children of my player. How do i fix this?

0
Well, you're telling the ray to find player.Character when you use FindPartOnRay(). You should try to assign parameter 2 to a generic rig and not a locked one like player.Character. DeceptiveCaster 3761 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Hello there!

I would suggest doing this on line 14.

if part then
    local humanoid = part:FindFirstChild('Humanoid')
    if humanoid then
        print("hit")
    end
end

I don't suggest using .Touched since it's very junky and unreliable. Raycast parts might do a better job at that, it's more reliable.

Happy coding!

Ad

Answer this question