I'm trying to have a better/shorter way to decrease any lag and use less effort, as if I need to change anything in the script, I'll need to do it for each one.
Is there a better way of having short?
I tried having them the "Glass1's" the same name and the "Glass2's" the same name, but it worked on the first one only, I wish I clarified it.
Here is my code:
local End = script.Parent.End
local Start = script.Parent.Start
local Glass = script.Parent
--Glass1/1-8 are the glasses that fall if touched and they change color to red
local function TouchedGlass11(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass11
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass11.Touched:Connect(TouchedGlass11)
local function TouchedGlass12(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass12
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass12.Touched:Connect(TouchedGlass12)
local function TouchedGlass13(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass13
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass13.Touched:Connect(TouchedGlass13)
local function TouchedGlass14(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass14
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
else return
end
end
Glass.Glass14.Touched:Connect(TouchedGlass14)
--then I'll do Glass2/1-8 which just turn the brick to green.
local function TouchedGlass21(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass21
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass21.Touched:Connect(TouchedGlass21)
local function TouchedGlass22(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass22
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass22.Touched:Connect(TouchedGlass22)
local function TouchedGlass23(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass23
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass23.Touched:Connect(TouchedGlass23)
local function TouchedGlass24(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = Glass.Glass24
num.BrickColor = BrickColor.Green()
else return
end
end
Glass.Glass24.Touched:Connect(TouchedGlass24)
--Does anyone know a better way?
please note: there is 8 glasses in both "Glass2" and "Glass1" but I'll be adding more soon, that's why I'm looking for an easier way.
Looking at your code it's pretty easy to spot that your using a lot of touched functions.
You can condense this significantly by using a for loop which iterates through each of the parts in script.Parent.
local glass = script.Parent
-- iterate through each child and assign the child to the variable object
for _, object in pairs(glass:GetChildren()) do
-- Make sure this child of script.Parent is actually a part.
if object:IsA("Part") then
object.Touched:Connect(function(hit)
local partParent = hit.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local num = object
num.Anchored = false
num.BrickColor = BrickColor.Red()
wait(2)
num:Destroy()
end
end)
end
end
You also shouldn't need to have a return in your touched functions.