Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Gun script isnt damaging when it hits a players accessory?

I have been extremely modifying a gun script and the last thing that I have to change is so when a bullet hits a players accessory it still damages them.

But at the moment it doesnt so, lets say a player has a backpack on. If they get hit in the backpack nothing happens.

Heres the code, these are just 2 chunks of the code, but its the part that does the damage.

LOCAL SCRIPT

if not Module.ExplosiveEnabled then
			if Hit and Hit.Parent then
				local TargetHumanoid = Hit.Parent:FindFirstChild("Humanoid")
				local TargetTorso = Hit.Parent:FindFirstChild("UpperTorso")
				local TargetAddon = Hit.Parent:FindFirstChildOfClass("Accessory")
				--local TargetTEAM = Hit.Parent:FindFirstChild("TEAM")
				if TargetHumanoid and TargetHumanoid.Health > 0 and TargetTorso and TargetAddon then
					--if TargetTEAM and TargetTEAM.Value ~= TEAM.Value then
						InflictTarget:FireServer(TargetHumanoid,
												TargetTorso,
												TargetAddon,
												(Hit.Name == "Head" or Hit.Name == "Helmet" and Module.HeadshotEnabled) and Module.BaseDamage * Module.HeadshotDamageMultiplier or Module.BaseDamage,
												Direction,
												Module.Knockback,
												Module.Lifesteal,
												Module.FlamingBullet)
						PiercedHumanoid[TargetHumanoid] = true
					--end
				else
					PierceAvailable = 0
				end
			end
		else

SERVER SCRIPT

InflictTarget.OnServerEvent:connect(function(Player,TargetHumanoid,TargetTorso,TargetAddon,Damage,Direction,Knockback,Lifesteal,FlamingBullet)
	if Player and TargetHumanoid and TargetHumanoid.Health ~= 0 and TargetTorso and TargetAddon then
		while TargetHumanoid:FindFirstChild("creator") do
			TargetHumanoid.creator:Destroy()
		end
		local creator = Instance.new("ObjectValue",TargetHumanoid)
		creator.Name = "creator"
		creator.Value = Player
		game.Debris:AddItem(creator,5)
		TargetHumanoid:TakeDamage(Damage)  
		if Knockback > 0 then
			TargetTorso.Velocity = Direction * Knockback
		end
		if Lifesteal > 0 and Humanoid and Humanoid.Health ~= 0 then
			Humanoid.Health = Humanoid.Health + (Damage*Lifesteal)
		end
		if FlamingBullet then
			local Debuff = TargetHumanoid.Parent:FindFirstChild("IgniteScript") or script.IgniteScript:Clone()
			Debuff.creator.Value = Player
			Debuff.Disabled = false
			Debuff.Parent = TargetHumanoid.Parent
		end
	end
end)

Youll notice ive modified it myself or attempted at it, I added TargetAddon to both of the scripts but that doesnt do anything.

Thank you for your time, hopefully some one may figure this out.

Allow me to add the fact that the accessory is made of parts, not a mesh. The gun script works with accessories that are made of meshes but not ones that are made of parts.

2 answers

Here's a piece of script from a gun i've been working on. You could try something like this for the hit detection:

 if hit then
                local humanoid =  hit.Parent:FindFirstChild("Humanoid")
                if humanoid then
                    if hit.Name == "Head" then
                        --//Double damage on headshots
                        humanoid:TakeDamage(dmg.Value*2)
                    else
                        --//Normal Damage on body shots
                        humanoid:TakeDamage(dmg.Value)
                    end
                end
            end

No matter where it hits on the player/NPC, it searches for a humanoid from the parent of the part you hit, and damages the humanoid

4 comments

0

But its hitting a part inside an accessory. So the parent of the part is the accessory.

0

Plus isnt that what my script is already doing??

0

Try making it check if hit.Parent is an accessory, if it is then it searches the parent of the accessory

0

It works when the accessory is made of meshes but when its made of parts it doesnt work.

iiDrac0's method would only partially work, because it searches for a humanoid in the parent of the part it hit. If it hit the Handle of an accessory, the parent would be the accessory, not the charater that the humanoid is found in.

The way that I use is to check the players from the Players service, look at their character, and see which character the brick is a descendant of (or, which character is an ancestor of the part that was hit; same thing.)

local TargetHumanoid = nil
for i,v in pairs(game.Players:GetPlayers())do
	if v and v.Character and v.Character:IsAncestorOf(Hit)then
		TargetHumanoid = v.Character:FindFirstChild("Humanoid")
		break
	end
end

1 comment

0

It works with normal accesories thought, ones that are meshes.

This discussion is preserved as it was. New answers, comments, and votes are closed.