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

How to check if the name of a part starts with a certain phrase?

Asked by
Podnf 22
7 years ago

I am making a disco floor and in the disco floor's code I am doing this:

for i,v in pairs(script.Parent.Parts:GetChildren()) do
    if v.Name == "discoPart" then
        -- Insert disco code here
    end
end

I want to make my disco floor more sophisticated, but not put a script in every block, so how do I check if the part name starts with disco but then it doesn't matter what else follows?

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Use string.sub like so.

for i, v in pairs(script,Parent.Parts:GetChildren()) do
    if string.sub(v.Name, 1, 5) == 'disco' then
        --insert stuff here
    end
end
Ad
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago

By using the sub[string] method of the string library.

for i,v in pairs(script.Parent.Parts:GetChildren()) do
    if v.Name:sub(1,5) == "disco" then
        -- Insert disco code here
    end
end

Answer this question