Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

How to make one script do the work for many parts?

  • This website was created by Roblox members to replace the official forums (because Roblox shut them down) Join us if you like Roblox!

xDShadowhawk

New Member
Reputation: 0 / 0 / 0
Oct 14, 2021
2
0
1
Roblox
xDShadowhawk
Greetings,

I am having trouble with my scripting, (Because i am new), Basically i need 100 parts to do one job, but i dont wanna right 100 scripts for each part. is there any way i can make one script that does the job of all the parts?

i need a parts to be destroyed when you touch them, i have that part down. but there is 100 of the same part that does the same job. how can i make 1 script for all 100 parts?
 

CallMeKY

Moderator
Ultra
Reputation: 8 / 0 / 0
Feb 25, 2018
1,749
94
866
113
Maine, USA
Roblox
CallMeKY
Discord
CallMeKY#5012
Greetings,

I am having trouble with my scripting, (Because i am new), Basically i need 100 parts to do one job, but i dont wanna right 100 scripts for each part. is there any way i can make one script that does the job of all the parts?

i need a parts to be destroyed when you touch them, i have that part down. but there is 100 of the same part that does the same job. how can i make 1 script for all 100 parts?
How exactly you achieve it is different depending on the situation, but basically you'd iterate through all of the parts. One of the most straight-forward ways to do that is to store every applicable part in a folder in workspace. If you can't do that, another way would be to name the parts something unique and iterate through descendants of each applicable model and find the parts with that name.

parts in folder example:
local function connectToPartsInFolder(folder)
    for _, part in pairs(folder:GetChildren()) do
        part.Touched:Connect(function(hitPart)
            local player = game:GetService('Players'):GetPlayerFromCharacter(hitPart:FindFirstAncestorOfClass('Model'))
            if not player then return end
            part:Destroy()
        end)
    end
end

Lua:
local function findNamedPartInContainer(container, partName)
    for _, object in pairs(container:GetDescendants()) do
        if object.Name == partName then
            object.Touched:Connect(function(hitPart)
                local player = game:GetService('Players'):GetPlayerFromCharacter(hitPart:FindFirstAncestorOfClass('Model'))
                if not player then return end
                object:Destroy()
            end)
        end
    end
end
 
Last edited:
Solution

xDShadowhawk

New Member
Reputation: 0 / 0 / 0
Oct 14, 2021
2
0
1
Roblox
xDShadowhawk
Greetings,

I am having trouble with my scripting, (Because i am new), Basically i need 100 parts to do one job, but i dont wanna right 100 scripts for each part. is there any way i can make one script that does the job of all the parts?

i need a parts to be destroyed when you touch them, i have that part down. but there is 100 of the same part that does the same job. how can i make 1 script for all 100 parts?
How exactly you achieve it is different depending on the situation, but basically you'd iterate through all of the parts. One of the most straight-forward ways to do that is to store every applicable part in a folder in workspace. If you can't do that, another way would be to name the parts something unique and iterate through descendants of each applicable model and find the parts with that name.

parts in folder example:
local function connectToPartsInFolder(folder)
    for _, part in pairs(folder:GetChildren()) do
        part.Touched:Connect(function(hitPart)
            local player = game:GetService('Players'):GetPlayerFromCharacter(hitPart:FindFirstAncestorOfClass('Model'))
            if not player then return end
            part:Destroy()
        end)
    end
end

find named parts example:
local function findNamedPartInContainer(container, partName)
    local parts = {}
    for _, object in pairs(container:GetDescendants()) do
        if object.Name == partName then
            table.insert(parts, object)
        end
    end
    for _, part in pairs(parts) do
        part.Touched:Connect(function(hitPart)
            local player = game:GetService('Players'):GetPlayerFromCharacter(hitPart:FindFirstAncestorOfClass('Model'))
            if not player then return end
            part:Destroy()
        end)
    end
end


Thankyou, this has helped a lot, and will save me a lot of time when i need to make 1 script replace 100. :)