My Script to make a certain number of block take health away are not working

So I have a few blocks in my game that I want so when you touch it you take damage. I don’t know if this is possible to do it in table form but I did it and I got an error.

This is how it looks in explorer
Error12

This is the script

local loc = script.Parent
local deb = false

local Parts = {
	loc.Bottom1,
	loc.Bottom2,
	loc.Bottom3,
	loc.Bottom4,
	loc.Bottom5
	
}


Parts.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and not deb then
		deb = true
		h.Health -= 20 -- "-=" instead of just "-"
		wait(1)
		deb = false
	end
end)
2 Likes
local loc = script.Parent
local deb = false

for i, part in pairs(loc:GetChildren()) do
if part:IsA("BasePart") then
Parts.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and not deb then
		deb = true
		h.Health -= 20 -- "-=" instead of just "-"
		wait(1)
		deb = false
	end
end)
end

Not tested, I am on mobile.

Would this get all the parts in the folder?

Yes, UnionOperations, MeshParts, TrussParts etc.

I have other parts in the folder as well that are not meant to give you damage.

I just need the Parts that I put in the table from the above script

Then I guess you should name every part that gives damage into “Bottom” and go with the following script:

local loc = script.Parent
local deb = false

for i, part in pairs(loc:GetChildren()) do
if part.Name == "Bottom" then
Parts.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and not deb then
		deb = true
		h.Health -= 20 -- "-=" instead of just "-"
		wait(1)
		deb = false
	end
end)
end
1 Like

I get a few errors with that script

It may be how the ends are placed, isn’t it?

1 Like

Maybe let me try and see if thats the problem

This is an undifined varaible.

Ah, sorry.

local loc = script.Parent
local deb = false

for i, part in pairs(loc:GetChildren()) do
if part.Name == "Bottom" then
part.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h and not deb then
		deb = true
		h.Health -= 20
		wait(1)
		deb = false
	end
end)
end
1 Like

You just need to add one more end then it works perfectly!

1 Like