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

First time making a weapon, What do i do?

Well the title states that this is a request, but it's not, i'm having trouble finding a characters humanoid and damaging it on hit, This is my script, its in a serverscript in a tool, Whats wrong?

local damage = script.Parent.Configuration.Damage.Value

script.Parent.Part.Touched:Connect(function()
	local hum = script.Parent.Part:FindFirstChildOfClass("Humanoid")
	hum.Health = - damage
end)

Thanks for reading,

2 comments

0

umm where is this script located?

0

In the tool itself

1 answer

✓ Accepted answer

Well, first of all, (almost) every :Connect(function() returns a argument in "function(arg1)" (which of, arg1 is the argument it returns.). In this case, the :Connect(function(arg1) returns the part being hit. (:Connect(function(partBeingHit)). So, knowing that, you can do:

local damage = script.Parent.Configuration.Damage.Value
 
script.Parent.Part.Touched:Connect(function(partBeingHit)
	local hum = partBeingHit.Parent:FindFirstChild("Humanoid") --most of the cases, the partBeingHit is a leg, a arm, the torso or the head. These limbs are inside a model (thats the player in some cases) and, inside the model, theres the Humanoid. Knowing that, the limbs' parent will be the player model, and, if theres a humanoid in it (some cases there arent), it will subtract the health.
	if hum then
		hum.Health = - damage
	end
end)

But wait, theres a problem here: A error occours in line 6. Well, the maths here are wroten wrong.

if hum then
	hum.Health = - damage --wroten wrong
end

the correct way would be:

if hum then
	hum.Health = hum.Health - damage --sets the hum.Health as hum.Health and subtracts it by the damage
end

that works. heres the fixed version, without all the explaining:

local damage = script.Parent.Configuration.Damage.Value
 
script.Parent.Part.Touched:Connect(function(partBeingHit)
	local hum = partBeingHit.Parent:FindFirstChild("Humanoid")
	if hum then
		hum.Health = hum.Health - damage
	end
end)

Hope that helped.

2 comments

0

You also could use hum:TakeDamage(damage)

0

yep

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