My gun, after successfully hitting an enemy, can randomly (uncommon) revert the damage that was done. So if I shoot someone who has 100 health, it would deal 30, then quickly revert it back to 100. I don't really know why this occurs - can someone shed some light? Could it be due to the health regeneration script in NPCs that I'm shooting causing conflict? My gun's script is a local script, and the damage dealing section is below.

missile.Touched:connect(function(hit)
		if hit.CanCollide ~= false or hit.Name == "Right Leg" or hit.Name == "Left Leg" or hit.Name == "Left Arm" or hit.Name == "Right Arm" then
			if hit.Transparency ~= 1 then
				if hit == game.Workspace.Terrain then 
					--NOTE: If the rocket is large, then the simplifications made here will cause it to fly through terrain in some cases
					local frontOfRocket = missile.Position + (missile.CFrame.lookVector * (missile.Size.Z / 2))
					local cellLocation = game.Workspace.Terrain:WorldToCellPreferSolid(frontOfRocket)
					local cellMaterial = game.Workspace.Terrain:GetCell(cellLocation.X, cellLocation.Y, cellLocation.Z)
					if cellMaterial == Enum.CellMaterial.Water or cellMaterial == Enum.CellMaterial.Empty then
						local splashnoise = game.ReplicatedStorage.SoundEffects.Splash:Clone()
						splashnoise.Parent = missile
						splashnoise:Destroy()
						SetProperties(missile) -- Workaround for weird physics when shooting in water!
						return 
					end
				end
				 -- The above could lag?
				local explosionsound = Gun.Handle.ExplosionSound:Clone()
				explosionsound.Parent = missile
				explosionsound.PlayOnRemove = true
				missile:Destroy() 
				if not hashit then
					hashit = true
					local E = Instance.new("Explosion")
					E.BlastPressure = Pressure.Value
					E.BlastRadius = Radius.Value
					E.DestroyJointRadiusPercent = Percent.Value
					E.ExplosionType = Type.Value
					E.Position = missile.Position 
					E.Parent = game.Workspace
					E.Hit:connect(function(hitPart, partDistance)
						if (not hitPart:IsDescendantOf(Character)) and hitPart.Name == "Torso" then
							print("Hit "..hitPart.Name.."!")
					    	local humanoid =  hitPart.Parent:FindFirstChild("Humanoid")
					    	if humanoid and humanoid.Health > 0 and IsEnemy(humanoid) then
						        local distance_factor = partDistance / E.BlastRadius    -- get the distance as a value between 0 and 1
						        local distinvert = 1 - distance_factor
								local newdamage = damage*distinvert
								hitpeople = hitpeople + 1
								if hitpeople >= 3 then
									local hitsound = Loadout.HitSound:Clone()
									hitsound.PlaybackSpeed = (100 - newdamage)/80
									hitsound.Parent = Gui_Clone
									hitsound.PlayOnRemove = true
									hitsound:Destroy() -- flip the amount, so that lower == closer == more damage
									print("PlayedSound")
								end
						        humanoid:TakeDamage(newdamage) -- 0: no damage; 1: max damage
								if humanoid.Health <= 0 then
									local speedvariant = math.floor(math.random()*3)+1
									if speedvariant == 1 then
													Player.PlayerGui.Equipment.Loadout.KillSound.PlaybackSpeed = 1
									elseif speedvariant == 2 then
										Player.PlayerGui.Equipment.Loadout.KillSound.PlaybackSpeed = 1.5
									elseif speedvariant == 3 then
										Player.PlayerGui.Equipment.Loadout.KillSound.PlaybackSpeed = 2
									end
									Player.PlayerGui.Equipment.Loadout.KillSound:Play()
								end
					    	end
						end
					end)
					if hashit == true then
						hashit = false
					end
				else
					print("Already hit.")
				--HitSound:Play()
				end
			end
		end
		hashit = false
	end)	
end

Thanks!

2 comments

0

I found this to be a common problem when dealing damage with the touched function. Your best bet would be to use a raycast to deal damage and the projectile will only be cosmetic.

0

My weapon is a rocket launcher and the projectile is quite slow - is there any other way?