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

How can i make this script simpler?

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)

2 answers

✓ Accepted answer

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

5 comments

0

model means that model that the parts are in yh??

0

yup

0

thank you so much buddy this helped me out fineally this website helped me out and for the first time it didnt say that it was a request

0

Please explain your code instead of just posting the code itself.

1

there is nothing to explain in that code. i told you what i needed. how to make the code simpler and deadly already told me that

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