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

How to change somebody's Health with a Projectile that touches a player?

What I tried:

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()


function Fired()
	
		
		local bullet = Instance.new("Part", script.Parent)
		bullet.CFrame = script.Parent.Handle.CFrame
		local bulletmesh = Instance.new("CylinderMesh", bullet)
		bullet.Size = Vector3.new(0.9, 0.4, 0.4)
		bullet.Rotation = Vector3.new(0, 0, -96.521)
		local b = Instance.new("BodyPosition", bullet)
		bullet.Name = "Projectile"
		bullet.BrickColor = BrickColor.Black()
		b.maxForce = Vector3.new(100000,100000,100000)
		b.Position = mouse.Hit.p
		
	--THIS PART	
	function onhit(hit)
		if bullet.Position == mouse.Target.Position then
			mouse.Target.Humanoid.Health = mouse.Target.Humanoid.Health - 20
		end
	end
	bullet.Touched:connect(onhit)

	
end

mouse.Button1Down:connect(Fired)

And it failed.. can you help me?

2 answers

✓ Accepted answer

When you fire a projectile you want it to continue to move in that direction for some period of time until it hits something. During this period of time, your mouse can be doing something else and it is not necessarily looking at the target. Therefore it doesn't make sense to damage the mouse target.

What you should be doing is damaging the thing that is hit by the projectile. In your case, you want to check to see if the thing that was hit was a character, and decrease their health if it was.

We also probably want the projectiles to be destroyed when they hit something. It would also be useful to make them be deleted after a set amount of time, as there is the possibility that they could never touch anything and thus waste computations (potentially creating lag). The Debris service is nice for accomplishing this:

local mouse = game.Players.LocalPlayer:GetMouse()
local debris = game:GetService("Debris")

mouse.Button1Down:connect(function()
	local bullet -- create the projectile

	bullet.Touched:connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(20)
		end
		bullet:Destroy()
	end)

	debris.AddItem(bullet, 5)
end)

I'm not sure what the heck you're trying to do with all the mouse.Target stuff but here are a few things you should know to understand why you aren't taking the health of your target:

mouse.Target can only return parts that the mouse is facing. The Humanoid is contained in the character model, not a part. Meaning that mouse.Target.Humanoid will always be nil.

So your issue aside from whether or not the conditional checking whether or not the bullet's position is the same as a targeted part is that you aren't even trying to take health from something that exists.

Replace mouse.Target.Humanoid.Health = mouse.Target.Humanoid.Health - 20 with hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 20 and hopefully your code will work.

Hope this helped.

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