Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

First time making a weapon, What do i do?

Asked by 4 years ago

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,

0
umm where is this script located? User#32819 0 — 4y
0
In the tool itself EllaTheFloofyFox 106 — 4y

1 answer

Log in to vote
1
Answered by
RAFA1608 543 Moderation Voter
4 years ago

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.

0
You also could use hum:TakeDamage(damage) Geobloxia 251 — 4y
0
yep RAFA1608 543 — 4y
Ad

Answer this question