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

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

(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

✓ Accepted answer

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

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 :)

1 comment

0

thank you!!!

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