Checking All Parts For Neon When Touched

I’m new to scripting and I’m trying to get it to when any part is touched the script checks if the part is neon and if it is it will take away health.

-Here’s the script

local allParts = game.Workspace:GetChildren(“Part”)

allParts.Touched:Connect(function(OnTouch)
local char = OnTouch.Parent
local humanoid = char:FindFirstChildWhichIsA(“Humanoid”)
if humanoid then

if allParts.Material == Enum.Material.Neon then
humanoid.Health -= 50
end
end
end)

(the error is ServerScriptService.LavaScript:3: attempt to index nil with ‘Connect’ - Server - LavaScript:3)

1 Like

Use a loop.

Code:

for i, part in workspace:GetChildren() do
	if part:IsA("BasePart") and part.Material == Enum.Material.Neon then
		part.Touched:Connect(function(OnTouch)
			local char = OnTouch.Parent
			local humanoid = char:FindFirstChildWhichIsA("Humanoid")
			
			if humanoid then
				humanoid.Health -= 50
			end
		end)
	end
end
1 Like

The error went away but it wont take away health.

i think there may be a problem with the if/then statement, idk tho.

nvm i got, thanks for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.