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)
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.
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.
-- 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)
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)
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
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.
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.