function ontouch(hit) if hit.Parent:findFirstChild("Humanoid") ~= nil then local model = game.Workspace.Parts model.Part1.Material = "Neon" model.Part2.Material = "Neon" model.Part3.Material = "Neon" model.Part4.Material = "Neon" model.Part5.Material = "Neon" model.Part6.Material = "Neon" model.Part7.Material = "Neon" model.Part8.Material = "Neon" model.Part9.Material = "Neon" model.Part10.Material = "Neon" model.Part11.Material = "Neon" model.Part12.Material = "Neon" model.Part13.Material = "Neon" model.Part14.Material = "Neon" model.Part15.Material = "Neon" model.Part16.Material = "Neon" model.Part17.Material = "Neon" model.Part18.Material = "Neon" model.Part19.Material = "Neon" model.Part20.Material = "Neon" end end script.Parent.Touched:connect(ontouch)
You don't need to assign a variable to a function you are only accessing in one place.
Assuming Part1-Part20 are the only contents of the model, you can just iterate through all of its children.
~= nil
is redundant in this case.
model
should be defined outside of the function instead of creating the variable every time the function is called.
game.Workspace
can be shortened to workspace
.
local parts=workspace.Parts:GetChildren() script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid")then for _,v in pairs(parts)do v.Material="Neon" end end end)
for _,v in pairs (model:GetChildren())do v.Material = "Neon" end