Bullet hit detection system

Hello devforum users I have made this script where its supposed to detect if the player is hit by a part named “Bullet” Unfortunately it doesn’t work for some apparent reason. The script these guns use is just a modified version of this devhub script.

https://developer.roblox.com/en-us/onboarding/hit-detection-with-lasers/1

local humanoid = script.Parent:WaitForChild("Humanoid")

humanoid.Touched:Connect(function(hit)
	if hit.Name == "Bullet" then
		print("player hit by bullet")
	end
end)
1 Like

Humanoid is an Instance, okay, But not a Part or BasePart, you know what I mean?

Part is a brick, that can be touched and be used for other events.
The BasePart is also a brick, but it has a complex problem that I dont know how to explain right, but you have to search.

So basically, Use a Part, Not an Instance that gives Property to a Rig/Character.

Example →

yourbodypart.Touched:Connect(function(hit)
        if hit.Name == 'Bullet' then
              print('oh no it got hit by a bullet!')
        end
end)

Also, I have a better solution, Try this, and use by putting a script on the bullet, or if the bullet is being created by Instance.new, just use yourNewBullet.Touched.

Bullet.Touched:Connect(function(hit)
       if hit.Parent:FindFirstChild('Humanoid') then
            --[ bla bla..
       end
end)

Reply if worked!

Humanoid.Touched does exist
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Touched

1 Like

For the first script, would I put this into startercharacterscripts?
edit update: the first script doesn’t work.

local humanoid = script.Parent:FindFirstChild("Humanoid")
local human = script.Parent

--body parts
local LArm = human["Left Arm"]
local RArm = human["Right Arm"]
local Lleg = human["Left Leg"]
local RLeg = human["Right Leg"]
local Torso = human.Torso
local Head = human.Head

Torso.Touched:Connect(function(hit)
	if hit.Name == "Bullet" then
		print("ahhh")
	end
end)

The issue with the second script is that the manager that detects wether the bullet has hit a player uses a magnitude/proximity as shown here. Like said this is just a somewhat modified version of the devhub tutorial


local function isHitValid(playerFired, characterToDamage, hitPosition)
	-- Validate distance between the character hit and the hit position
	local characterHitProximity = (characterToDamage.HumanoidRootPart.Position - hitPosition).Magnitude
	if characterHitProximity > MAX_HIT_PROXIMITY then
		return false
	end

I think its a lot more easier to detect when the >Bullet is touched<.

Since its trying to give damage, Why try to detect in multiple body parts?
So when the bullet is touched, If your trying to deal damage, Remove the Humanoid Health by your preference number!

1 Like

Well, Im making an injury’s system and I need it to detect where it hit. Such as, if the bullet hit your arm, you can’t use your weapon cause you have a bullet injury. Or such as when your leg gets shot you’ll be walking even slower.

You can make a tag function in the hit, Like the bullet hits the body Part, it creates a tag, and when the character try to detect which body part is tagged, you can print just to do a test.

So you just need to make a script in StarterCharacterScripts, that does a while wait() do function, and searchs for the tagged part, using :GetChildren().

local IsDamaged = false -- [ You can also implement variables to set which Body Part is damaged.
function test()
      local Children = script.Parent:GetChildren()
      for _,part in pairs(Children) do
             if part:FindFirstChild('Tag') and (not IsDamaged == true) then -- [ debounce
                      IsDamaged = true
             end
      end
end

while wait() do
     test()
end

After It detects the tag exists, Then you do whatever you want.

And when the bullet is touched →

local bullet = script.Parent

function tag(char)
       local tag = Instance.new('IntValue',char)
       tag.Name = 'Tag'
end

local TaggedPlayers = {}

bullet.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild('Humanoid') and not (table.find(TaggedPlayers,hit.Parent.Name) then -- [ debounce
                 tag(hit.Parent)
                 table.insert(TaggedPlayers,hit.Parent.Name)
        end
end)

About your injury system, you can set a BoolValue to the Player, That is IsDamaged, So It can be local and you can use in multiple scripts.

1 Like

I added a print statement and It doesn’t seem to work.

local IsDamaged = false -- [ You can also implement variables to set which Body Part is damaged.
function test()
	local Children = script.Parent:GetChildren()
	for _,part in pairs(Children) do
		if part:FindFirstChild('Tag') and not IsDamaged then -- [ debounce
			IsDamaged = true
			print("hit")
		end
	end
end

while wait() do
	test()
end

you forgot to set if is damaged is not true

local IsDamaged = false -- [ You can also implement variables to set which Body Part is damaged.
function test()
		if script.Parent:FindFirstChild('Tag') and not (IsDamaged == true) then -- [ debounce
			IsDamaged = true
			print("hit")
		end
end

while wait() do
	test()
end

i usually put paranthesis to use the not, It usuallys gives some errors, so I use like that, but try now

i forgot that the tag will not exist as a children of a body part, but the character/rig, so use this change.

1 Like

also, about the bullet parent, i putted like that if your not using Instance.new or Clone()

1 Like

Still isn’t printing whenever I fire at the player and it damages them.

print is kind of bugged right now, I also expected that, But Is it working properly?

1 Like

Wait so the script si supposed to parent the tag onto the player that was hit the bullet correct?

yes, it creates a tag for the character selected, only if theres no tags already added before it, thats kinda used for fighting tools, which you might wanna get money from npcs, which is Not the case.

1 Like

Well it doesn’t seem to be working.

Can you show the script? If you dont mind

Which one? (the extra word litmijts)

The script for the bullet, and the script for the character

1 Like
local IsDamaged = false -- [ You can also implement variables to set which Body Part is damaged.
function test()
	if script.Parent:FindFirstChild('Tag') and not (IsDamaged == true) then -- [ debounce
		IsDamaged = true
		print("hit")
	end
end

while wait() do
	test()
end
local bullet = script.Parent

function tag(char)
	local tag = Instance.new('IntValue',char)
	tag.Name = 'IsPlayerHit'
end

local TaggedPlayers = {}

bullet.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not table.find(TaggedPlayers,hit.Parent.Name) then
		tag(hit.Parent)
		table.insert(TaggedPlayers,hit.Parent.Name)
	end
end)