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

Help! How to give parts with certain properties names, automatically?

So in workspace, If any object is a really red ball I want it to be named "Lock On!". How would I be able to do that?

I tried making the script below but it didn't work! Help!

--This is in a LocalScript
game:GetService('RunService'):Connect(function()
for _, child in pairs(game.workspace:GetChildren()) do
    if child.ClassName == 'Part' and child.BrickColor == "Really red" then
       child.Name = "Lock on!"
    end
end
end)

7 comments

0
  1. Are you sure that the part you're looking for is already in the workspace? And 2. Is it parented directly under workspace, or under something else that's in workspace?
0

Also, try using child:IsA("Part") instead, and change "Really Red" into BrickColor.new("Really red")

0

Lastly, and this might be the cause. but you're connecting to RunService directly, not to something like Stepped or RenderStepped.

0

That is inefficient to just check that every FRAME. Not like you need to check this 60 times a second, 3,600 times a minute. Also, how hard is this for you guys to indent properly?

0

Also, if you are going to instance a ball (assuming you did not insert it in studio and just like NOT RENAME IT), why cant u just rename the part while making it? Does not make sense.

0

@himmgoodpack The point of the script is to give names to parts of those properties automatically. Without naming it in Studio.

0

Why? This makes lag for things that is preventable if YOU JUST NAMED THEM IN STUDIO.

1 answer

This script should work for you. You need to use IsA instead of using ClassName and using RunService will spam Lock On.

for i,c in pairs(game.Workspace:GetChildren()) do
        if c:IsA("Part") then --Use "IsA" instead
               if c.BrickColor == "Really red" then
                     print("Lock On")
end 
end
end

I'm on mobile so I apologize for poor indentation. Please accept my answer if this helped!

4 comments

0

Remember to remind him to not check 60 times a second, 3,600 times a minute.

0

@hiimgoodpack One would say RenderStepped is a no brainer, cause it keeps things smooth

1

You should use DescendantAdded or ChildAdded.

0

Agreed^^, GetChildren() scales poorly if there are thousands of parts in workspace. Just do GetDesendants() once. If the part you're looking for isn't in GetDescendants(), then listen to DescendantAdded.

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