Help with making an npc take damage

So I’ve ran into an issue. I’m making an FPS game, and I want an npc to take damage when they get hit by a bullet. For some reason, it isn’t working. I tried putting the script inside the bullet already (with adjusted script) but it doesn’t work. Right now I’m trying to put it inside the npc, but this doesn’t work either, and i have no idea why.

-script

--inside humanoid of npc
script.Parent.Parent.Torso.Touched:Connect(function(Hit)
	if Hit.Name == "G17Bullet" then
		script.Parent:TakeDamage(10)
	end
end)

It would be awesome if someone could please help :smiley:

1 Like

How about posting the script that creates and sends the bullet?

1 Like

Is it a local script or a server script? AFAIK local scripts won’t cause damage to a Humanoid. I could be wrong, but I had issues with trying to play an animation and deliver damage to a player in the same script and the damage part of the script wasn’t working.

Also fast moving bullets using the Touched event will fail to touch every time due to the speed involved. Try using print("bullet touched") inside your Touched function to see if it’s actually being fired every time.

1 Like

its in a normal script, and i can change the speed of the bullet for testing.

-Edit
I tested it with very low bullet speed and it still didn’t work.

It might be that the Torso part of the humanoid isn’t being touched. You could try using Humanoid.Touched instead to detect if any limb is being touched. (Note that it would be more efficient to detect “touches” from the bullet, since Humanoid.Touched fires a lot when the humanoid walks/moves but it’s probably fine.)

I would bet though that the problem is the bullet is moving very fast. To get around this, you can use raycasting from the bullet in the direction it’s moving. This way it detects in the line that the bullet moves rather than based on the series of volumes of hitbox of the bullet as it moves.

how do I use raycasting?


You’d put a script in the bullet like this:

-- Generated with GPT 4o mini and edited --
-- Get the part from script.Parent
local part = script.Parent

-- Variable to store the previous position of the part
local previousPosition = part.Position

-- Function to perform the raycast
local function performRaycast()
    -- Raycast direction (current position - previous position)
    local direction = part.Position - previousPosition

    -- Create a ray starting from the previous position to the current position
    local rayOrigin = previousPosition
    local rayDirection = direction

    -- Perform the raycast
    local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

    -- Check if the ray hit something
    if raycastResult then
        print("Ray hit: " .. raycastResult.Instance.Name)
        -- TODO: Check if the part.Parent is a Model and if the part.Parent has a humanoid. If so, damage the humanoid
    else
        print("Ray did not hit anything.")
    end

    -- Update the previous position to the current one
    previousPosition = part.Position
end

-- Connect the raycast function to the Heartbeat event
local connection
connection = game:GetService("RunService").Heartbeat:Connect(function()
     if not script:IsDescendantOf(Workspace) then connection:Disconnect() end
    performRaycast()
end)

The script does not work

Does it print an error?
Try this as see if it prints anything

print("Start")
script.Parent.Parent.Torso.Touched:Connect(function(Hit)
print("Hit", Hit)
	if Hit.Name == "G17Bullet" then
		script.Parent:TakeDamage(10)
	end
end)
1 Like

Did it print bullet touched?

1 Like

I put this in with the bullets slowed down. It prints when I touch it, but never the bullet. Is it because the bullet is a union?

it maybe the bullet because of the velocity wont register the hit its to fast… this can happen sometime or used to bad with roblox physics… you can use raycasting etc to counter this sometimes

Make sure bullet.CanTouch = true and torso.CanTouch = true

And maybe use

script.Parent.Health -= 10

instead of

script.Parent:TakeDamage(10)

Is the function TakeDamage actually defined to Humanoid Health?

It doesn’t matter if the bullet’s a Union. Here are some questions you need to answer (please don’t just answer only one of them).

  • Do you have any of the Parts/Unions/bullets with the CanTouch Property set to true?

  • Is the bullet anchored and you CFrame it’s Position?

  • How big is the bullet?

I’d recommend using a raycast as others have said.

That isn’t a very helpful comment. You should explain where you placed the script. It should be in the bullet in the workspace and not in the bullet in ServerStorage or some other place. You could also have said it doesn’t print anything, or it only prints Ray did not hit anything to give us more information.

You can CanTouch enabled on both parts?

the bullet has can touch set to true, it is anchored with a CFramed position, and the bullet is pretty small.

I think I heard that Anchored parts that are CFramed don’t fire the Touched event.
You could test it by putting 2 parts in the workspace with Touched events scripted in them. CFrame the Anchored Part into the other part using a script to see if Touched fires.

1 Like

Ok thanks, I’ll try.

-Edit: It continues to not work. I have no idea why. No error messages.

1 Like

Any chance you could send me a file with the problem? I can fix this for you