You can use a for loop to get the specific parts from Workspace. There are 3 ways to go about this: 1. Have all the parts in a Folder located somewhere in Workspace; 2. Insert a BoolValue under each part (although it wouldn’t matter whichever value you’d choose); 3. Use CollectionService (although I don’t have enough experience with this service).
If you want to do Method 1, then insert a folder in Workspace, and parent all the parts under that folder. Then use a for loop to get all the children under that folder, and connect the Touched event to them.
Example:
local MainFol = workspace:WaitForChild("") -- locate your folder here
local canGet = true
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player and canGet then
canGet = false
player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value = player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value + 1
if otherPart:FindFirstChild("ParticleEmitter") then
otherPart.ParticleEmitter.Enabled = false
end
otherPart.Transparency = 1
task.wait(2)
if otherPart:FindFirstChild("ParticleEmitter") then
otherPart.ParticleEmitter.Enabled = true
end
otherPart.Transparency = 0
canGet = true
end
end
end
for _, parts in pairs(MainFol:GetChildren()) do
if parts:IsA("BasePart") then -- checks if the children under the folder is a 'BasePart'
parts.Touched:Connect(onTouch)
end
end
-- if you intend to add parts as the game runs, make sure to parent the new part(s) under that folder
MainFol.ChildAdded:Connect(function()
for _, parts in pairs(MainFol:GetChildren()) do
if parts:IsA("BasePart") then
parts.Touched:Connect(onTouch)
end
end
end)
Edit:
Now if you decide to do Method 2, then insert a BoolValue under each part involved in what you are trying to accomplish, and name them all the same thing (don’t mind the true/false Value property). Having a folder won’t be necessary. although I’d recommend it as it can actually be more organized.
Here is how your script would look like (without a folder):
local canGet = true
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player and canGet then
canGet = false
player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value = player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value + 1
if otherPart:FindFirstChild("ParticleEmitter") then
otherPart.ParticleEmitter.Enabled = false
end
otherPart.Transparency = 1
task.wait(2)
if otherPart:FindFirstChild("ParticleEmitter") then
otherPart.ParticleEmitter.Enabled = true
end
otherPart.Transparency = 0
canGet = true
end
end
end
for _, parts in pairs(workspace:GetDescendants()) do
if parts:IsA("BasePart") then -- checks if the descendants are a 'BasePart'
if parts:FindFirstChild("") -- put the name of the 'BoolValue' here
parts.Touched:Connect(onTouch)
end
end
end
-- if you intend to add parts as the game runs, make sure to include another 'BoolValue' in that part.
workspace.ChildAdded:Connect(function()
for _, parts in pairs(workspace:GetDescendants()) do
if parts:IsA("BasePart") then
if parts:FindFirstChild("") -- put the name of the 'BoolValue' here
parts.Touched:Connect(onTouch)
end
end
end
end)
Edit 2:
And here’s how it should look with a folder (for method 2):
local MainFol = workspace:WaitForChild("") -- locate your folder here
local canGet = true
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if player and canGet then
canGet = false
player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value = player.PlayerGui.Quest.ScoreBarFrame.TrainingAmount.AmountValue.Value + 1
if otherPart:FindFirstChild("ParticleEmitter") then
otherPart.ParticleEmitter.Enabled = false
end
otherPart.Transparency = 1
task.wait(2)
if otherPart:FindFirstChild("ParticleEmitter") then
otherPart.ParticleEmitter.Enabled = true
end
otherPart.Transparency = 0
canGet = true
end
end
end
for _, parts in pairs(MainFol:GetChildren()) do
if parts:IsA("BasePart") then -- checks if the children under the folder is a 'BasePart'
if parts:FindFirstChild("") -- put the name of the 'BoolValue' here
parts.Touched:Connect(onTouch)
end
end
end
-- if you intend to add parts as the game runs, make sure to include another 'BoolValue' in that part.
MainFol.ChildAdded:Connect(function()
for _, parts in pairs(MainFol:GetChildren()) do
if parts:IsA("BasePart") then
if parts:FindFirstChild("") -- put the name of the 'BoolValue' here
parts.Touched:Connect(onTouch)
end
end
end
end)
I don’t have enough experience with CollectionService, so I won’t be able to give an example for that.