Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can i make this script simpler?

Asked by 8 years ago
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

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

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)
Ad
Log in to vote
0
Answered by 8 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
for _,v in pairs (model:GetChildren())do
    v.Material = "Neon"
end
0
model means that model that the parts are in yh?? thegamingpro566 0 — 8y
0
yup TheDeadlyPanther 2460 — 8y
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 thegamingpro566 0 — 8y
0
Please explain your code instead of just posting the code itself. M39a9am3R 3210 — 8y
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 thegamingpro566 0 — 8y

Answer this question