.touched not working quite as needed

basically im trying to change the “Red hyperlaser” roblox gear into not instakilling and instead just doing damage to the npc being touched aand i ran into this issue: when the projectile hits the torso, instead of doing damage once, it damages the humanoid twice. i can see that it is because the projectile hit both the humanoidrootpart and the torso, so how would i go about making it only damage a certain character once, still being able to “penetrate” them?

heres the code, if you need any additional context just respond to this post

function Touched(Projectile, Hit)
	if not Hit or not Hit.Parent  then
		return
	end
	local character, humanoid = FindCharacterAncestor(Hit)
	if character and humanoid and character ~= Character and game.Players:GetPlayerFromCharacter(character) == nil then
		
		local ForceFieldExists = false
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("ForceField") then
				ForceFieldExists = true
			end
		end
		if not ForceFieldExists then
			if Projectile then
				humanoid:TakeDamage(Tool.Damage.Value)
				Projectile:Destroy()
				local HitFadeSound = Projectile:FindFirstChild(Sounds.HitFade.Name)
				local torso = humanoid.Torso
				if HitFadeSound and torso then
					HitFadeSound.Parent = torso
					HitFadeSound:Play()
				end
			end
			if humanoid.Health <= 0 then
				Dematerialize(character, humanoid, Hit)
			end
		end
		if Projectile and Projectile.Parent then
			Projectile:Destroy()
		end
	end
2 Likes

You can make a whitelist for that specific projectile to not hit the same character twice, or just ignore either hrp or torso hits

2 Likes

even if id ignore the torso/hrp it would still be able to multihit if you just for example aim at the legs. but about the whitelist option, how would i do that? dont have any expirience.

2 Likes

Adding hit character to a table for each projectile and checking if any hit character is found in the table to prevent damage for that char.

Also, iterating over character children to find the forcefield might be redundant, you can just use :FindFirstChildWhichIsA function instead

2 Likes

ill try that, thanks! also i should probably just delete the “checking for forcefield” thingy because it was just left over in the hyperlaser script

2 Likes

Touched isnt always working, same as animation events. its a known bug and roblox doenst fix it

2 Likes

worked, ive used table.insert and checked for character not being in table every time the function played. thanks!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.