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

Why do I have to repeat the same line of code when making a gradual damage brick?

Asked by 7 years ago
Edited 7 years ago

(lua newb)

function touch(damage)
    local rekt = damage.Parent:FindFirstChild("Humanoid").Health
    rekt = damage.Parent:FindFirstChild("Humanoid").Health -10
    wait(1)
end

script.Parent.Touched:connect(touch)

why do I have to repeat the same line of code? I know that it won't work otherwise but why?

damage.Parent:FindFirstChild("Humanoid").Health = damage.Parent:FindFirstChild("Humanoid").Health -10

2 answers

Log in to vote
1
Answered by 7 years ago

Hey NickLeBuilder,

You don't have to repeat the same code over and over. If I were you, I'd make a variable for the Humanoid of the Character and then use the :TakeDamage() method of the Humanoid. Below is a personal example.

local part = script.Parent; -- Set a variable for the part that will be touched;

part.Touched:Connect(function(obj) -- Anonymous function with the Touched event.
    local hum = obj.Parent:FindFirstChild("Humanoid"); -- Declares a variable for the 
    Humanoid(If there is one)
    if hum then -- Checks if it's a Character
        hum:TakeDamage(10); -- Takes away 10 health points.
    end -- end for if statement.
end) -- end for the anonymous function.

After that, the humanoid should lose 10 health.

Well, I hope I helped in one way or another. Have a great day/night. :)

~~ KingLoneCat

Ad
Log in to vote
0
Answered by
sad_eyez 162
7 years ago
Edited 7 years ago

I see that your trying to make a kill block, and I recommend using this type of code rather than that, here is a code that works pretty efficiently:

script.Parent.Touched:Connect(function(hit) -- Fires when part is touched
    if hit.Parent:IsA("Model") then -- Checks if the hit.Parent is a character
        local hum = hit.Parent:FindFirstChild("Humanoid") -- Finds the humanoid
        hum.Health= hum.Health - 10  --Takes away the health
    end
end)

Hope this helped :)

0
thank you!!! NickLeBuilder 13 — 7y

Answer this question