Anyway to make this script more efficient?

So I am making Obby, where the stages are invisible and appear when you touch a button. This is my button script and each variable - (I think it’s called a variable) is another block, the variable is linked to the part. Each part’s CanCollide and Transpanency is changed when the button is pressed and when the timer runs out.
Script:

script.Parent.BrickColor = BrickColor.new("Dark green")

startPosition = Vector3.new(-1.1, 0.9, 345.1) --Change to Coords off button top

local a = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part1
a.CanCollide = false
a.Transparency = 1

local b = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part2
b.CanCollide = false
b.Transparency = 1

local c = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part3
c.CanCollide = false
c.Transparency = 1

local d = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part4
d.CanCollide = false
d.Transparency = 1

local e = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part5
e.CanCollide = false
e.Transparency = 1

local f = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part6
f.CanCollide = false
f.Transparency = 1

local g = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part7
g.CanCollide = false
g.Transparency = 1

local h = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part8
h.CanCollide = false
h.Transparency = 1

local i = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part10
i.CanCollide = false
i.Transparency = 1

local j = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part11
j.CanCollide = false
j.Transparency = 1

local k = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part12
k.CanCollide = false
k.Transparency = 1

local l = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part13
l.CanCollide = false
l.Transparency = 1

local m = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part14
m.CanCollide = false
m.Transparency = 1

local n = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part15
n.CanCollide = false
n.Transparency = 1

local o = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part16
o.CanCollide = false
o.Transparency = 1

local p = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part9
p.CanCollide = false
p.Transparency = 1

local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top

local buttonactive = false

function displaytime(countdowntime)
	local timer = Instance.new ("BillboardGui", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top)
	timer.Size = UDim2.new(0,50,0,50)
	timer.StudsOffset = Vector3.new(0,5,0)
	timer.AlwaysOnTop = true
	timer.Enabled = true
	local obstruction = Button
	obstruction.Position = Vector3.new(-1.1, 0.4, 345.1) -- coords of button top (y-0.5)
	script.Parent.BrickColor = BrickColor.new("Really red")
	a.CanCollide = true 
	b.CanCollide = true 
	c.CanCollide = true 
	d.CanCollide = true 
	e.CanCollide = true 
	f.CanCollide = true 
	g.CanCollide = true 
	h.CanCollide = true 
	i.CanCollide = true 
	j.CanCollide = true 
	k.CanCollide = true
	l.CanCollide = true 
	m.CanCollide = true 
	n.CanCollide = true 
	o.CanCollide = true
	p.CanCollide = true
	a.Transparency = 0.75
	b.Transparency = 0.75
	c.Transparency = 0.75
	d.Transparency = 0.75
	e.Transparency = 0.75
	f.Transparency = 0.75
	g.Transparency = 0.75
	h.Transparency = 0.75
	i.Transparency = 0.75
	j.Transparency = 0.75
	k.Transparency = 0.75
	l.Transparency = 0.75
	m.Transparency = 0.75
	n.Transparency = 0.75
	o.Transparency = 0.75
	p.Transparency = 0.75
	wait (0.1)
	a.Transparency = 0.5
	b.Transparency = 0.5
	c.Transparency = 0.5
	d.Transparency = 0.5
	e.Transparency = 0.5
	f.Transparency = 0.5
	g.Transparency = 0.5
	h.Transparency = 0.5
	i.Transparency = 0.5
	j.Transparency = 0.5
	k.Transparency = 0.5
	l.Transparency = 0.5
	m.Transparency = 0.5
	n.Transparency = 0.5
	o.Transparency = 0.5
	p.Transparency = 0.5
	wait (0.1)
	a.Transparency = 0.25
	b.Transparency = 0.25
	c.Transparency = 0.25
	d.Transparency = 0.25
	e.Transparency = 0.25
	f.Transparency = 0.25
	g.Transparency = 0.25
	h.Transparency = 0.25
	i.Transparency = 0.25
	j.Transparency = 0.25
	k.Transparency = 0.25
	l.Transparency = 0.25
	m.Transparency = 0.25
	n.Transparency = 0.25
	o.Transparency = 0.25
	p.Transparency = 0.25
	wait (0.1)
	a.Transparency = 0
	b.Transparency = 0
	c.Transparency = 0
	d.Transparency = 0
	e.Transparency = 0
	f.Transparency = 0
	g.Transparency = 0
	h.Transparency = 0
	i.Transparency = 0
	j.Transparency = 0
	k.Transparency = 0
	l.Transparency = 0
	m.Transparency = 0
	n.Transparency = 0
	o.Transparency = 0
	p.Transparency = 0
	
	print("first displaytime sucess")
	local timertxt = Instance.new("TextBox", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top)
	timertxt.Parent = timer
	timertxt.Size = UDim2.new(0,50,0,50)
	timertxt.TextScaled = true
	timertxt.TextColor3 = Color3.new(255,255,255)
	timertxt.BackgroundColor3 = Color3.new(0,0,0)
	timertxt.BorderColor3 = Color3.new(0,0,0)
	timertxt.BackgroundTransparency = 1

	

	local bincountdown = countdowntime
	repeat
		timertxt.Text = tostring(bincountdown)
		bincountdown = bincountdown - 1
		wait(1)
	until
	bincountdown < 1
	
	
	obstruction.Position = Vector3.new(-1.1, 0.9, 345.1)
	timer.Enabled = false
	buttonactive = false
	a.CanCollide = false 
	b.CanCollide = false
	c.CanCollide = false
	d.CanCollide = false
	e.CanCollide = false 
	f.CanCollide = false
	g.CanCollide = false 
	h.CanCollide = false
	i.CanCollide = false
	j.CanCollide = false
	k.CanCollide = false 
	l.CanCollide = false
	m.CanCollide = false 
	n.CanCollide = false
	o.CanCollide = false
	p.CanCollide = false
	a.Transparency = 1	
	b.Transparency = 1
	c.Transparency = 1
	d.Transparency = 1
	e.Transparency = 1
	f.Transparency = 1
	g.Transparency = 1	
	h.Transparency = 1
	i.Transparency = 1
	j.Transparency = 1
	k.Transparency = 1
	l.Transparency = 1
	m.Transparency = 1
	o.Transparency = 1
	p.Transparency = 1


	script.Parent.BrickColor = BrickColor.new("Dark green")
	end
	
	local function onTouch(hit)
		if hit.Parent:FindFirstChild("Humanoid") then 
			if buttonactive == false then
				buttonactive = true
				displaytime(5) 
			else 
				print ("Error - Already active!")
			end
		end
	end
script.Parent.Touched:Connect(onTouch)
	

As you can see its very long and it kind of repeats itself. I was wondering if there was a way to do something like this:

a through to p, repeat
CanCollide = True
Transparency = 0

Making the script like this would make it much easier to modify when I copy and paste it for each stage and it’s button.
Thanks,

use for i, v in pairs like this:

     for i,v in pairs(game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"]:GetChildren()) do
     if v:IsA("Part") then  ----checking if its a part
     v.CanCollide= false
     v.Transparency = 1
     end

for i, v in pairs are like what you said

where v become the instance or value and i become the index

You can do something like this.

local Blocks = workspace:WaitForChild("Obby Structure"):WaitForChild("Levels 1-10"):WaitForChild("7"):WaitForChild("Blocks") -- Don't know if I misspelled anything here, make sure to check.

for i = 1, 16 do -- This loop will repeat 16 times.
    local Part = Blocks["Part"..tostring(i)] -- Get the part
    Part.CanCollide = true
    game:GetService("TweenService"):Create(Part, TweenInfo.new(0.3), {Transparency = 0}):Play() -- Tween the parts transparency to 0 in 0.3 seconds.
end

This will make the parts appear slowly.

use for i isnt that effective because you wont know how much part are there its more effective to use for i, v

1 Like

Trying to add that in has stopped the function from starting. No errors are appearing.

show us the approach you chose?

script.Parent.BrickColor = BrickColor.new("Dark green")

local Blocks = workspace:WaitForChild("Obby Structure"):WaitForChild("Levels 1-10"):WaitForChild("7"):WaitForChild("Blocks")
print("child found")
startPosition = Vector3.new(-1.1, 0.9, 345.1) --Change to Coords off button top

local a = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part1
a.CanCollide = false
a.Transparency = 1
print("a done")
local b = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part2
b.CanCollide = false
b.Transparency = 1

local c = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part3
c.CanCollide = false
c.Transparency = 1

local d = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part4
d.CanCollide = false
d.Transparency = 1

local e = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part5
e.CanCollide = false
e.Transparency = 1

local f = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part6
f.CanCollide = false
f.Transparency = 1

local g = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part7
g.CanCollide = false
g.Transparency = 1

local h = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part8
h.CanCollide = false
h.Transparency = 1

local i = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part10
i.CanCollide = false
i.Transparency = 1

local j = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part11
j.CanCollide = false
j.Transparency = 1

local k = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part12
k.CanCollide = false
k.Transparency = 1

local l = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part13
l.CanCollide = false
l.Transparency = 1

local m = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part14
m.CanCollide = false
m.Transparency = 1

local n = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part15
n.CanCollide = false
n.Transparency = 1

local o = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part16
o.CanCollide = false
o.Transparency = 1

local p = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"].Part9
p.CanCollide = false
p.Transparency = 1

local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top
print("button found")
local buttonactive = false

function displaytime(countdowntime)
	print("function started")
	local timer = Instance.new ("BillboardGui", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top)
	timer.Size = UDim2.new(0,50,0,50)
	timer.StudsOffset = Vector3.new(0,5,0)
	timer.AlwaysOnTop = true
	timer.Enabled = true
	local obstruction = Button
	obstruction.Position = Vector3.new(-1.1, 0.4, 345.1) -- coords of button top (y-0.5)
	script.Parent.BrickColor = BrickColor.new("Really red")
	print("upto new part")
	for i = 1, 16 do -- This loop will repeat 16 times.
		local Part = Blocks["Part"..tostring(i)] -- Get the part
		Part.CanCollide = true
		game:GetService("TweenService"):Create(Part, TweenInfo.new(0.3), {Transparency = 0}):Play() -- Tween the parts transparency to 0 in 0.3 seconds.
	


	print("first displaytime sucess")
	local timertxt = Instance.new("TextBox", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top)
	timertxt.Parent = timer
	timertxt.Size = UDim2.new(0,50,0,50)
	timertxt.TextScaled = true
	timertxt.TextColor3 = Color3.new(255,255,255)
	timertxt.BackgroundColor3 = Color3.new(0,0,0)
	timertxt.BorderColor3 = Color3.new(0,0,0)
	timertxt.BackgroundTransparency = 1



	local bincountdown = countdowntime
	repeat
		timertxt.Text = tostring(bincountdown)
		bincountdown = bincountdown - 1
		wait(1)
	until
	bincountdown < 1


	obstruction.Position = Vector3.new(-1.1, 0.9, 345.1)
	timer.Enabled = false
	buttonactive = false
	a.CanCollide = false 
	b.CanCollide = false
	c.CanCollide = false
	d.CanCollide = false
	e.CanCollide = false 
	f.CanCollide = false
	g.CanCollide = false 
	h.CanCollide = false
	i.CanCollide = false
	j.CanCollide = false
	k.CanCollide = false 
	l.CanCollide = false
	m.CanCollide = false 
	n.CanCollide = false
	o.CanCollide = false
	p.CanCollide = false
	a.Transparency = 1	
	b.Transparency = 1
	c.Transparency = 1
	d.Transparency = 1
	e.Transparency = 1
	f.Transparency = 1
	g.Transparency = 1	
	h.Transparency = 1
	i.Transparency = 1
	j.Transparency = 1
	k.Transparency = 1
	l.Transparency = 1
	m.Transparency = 1
	o.Transparency = 1
	p.Transparency = 1


	script.Parent.BrickColor = BrickColor.new("Dark green")
end

local function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then 
		if buttonactive == false then
			buttonactive = true
			displaytime(5) 
		else 
			print ("Error - Already active!")
		end
	end
	end
	
script.Parent.Touched:Connect(onTouch)

end

I chose to add the block variable at the top and the “for” in the function
(I added prints to see where the script was breaking)
I scripted it like this, I added the block variable with the others and i added the rest in the function as it was were the “manual” property lines were written.

You define Blocks but never use it

Also, it seems that you are using Obby Structure > Levels > 7 a lot.

Try using this instead:
local Stage = workspace:WaitForChild(“Obby Structure”):WaitForChild(“Levels 1-10”):WaitForChild(“7”)

When you need to use buttons, simply do Stage.Buttons

As for making the blocks visible,
Use the method MrchipsMa showed

@majdTRM @MrchipsMa @TypeUndefined
I altered the script to this:

script.Parent.BrickColor = BrickColor.new("Dark green")

local Stage = workspace:WaitForChild("Obby Structure"):WaitForChild("Levels 1-10"):WaitForChild("7")
print("child found")
startPosition = Vector3.new(-1.1, 0.9, 345.1) --Change to Coords off button top


local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top
print("button found")
local buttonactive = false

function displaytime(countdowntime)
	print("function started")
	local timer = Instance.new ("BillboardGui", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top)
	timer.Size = UDim2.new(0,50,0,50)
	timer.StudsOffset = Vector3.new(0,5,0)
	timer.AlwaysOnTop = true
	timer.Enabled = true
	local obstruction = Button
	obstruction.Position = Vector3.new(-1.1, 0.4, 345.1) -- coords of button top (y-0.5)
	script.Parent.BrickColor = BrickColor.new("Really red")
	print("upto new part")
	for i,v in pairs(game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"]:GetChildren()) do
		if v:IsA("Part") then  ----checking if its a part
			v.CanCollide = true
			v.Transparency = 0



	print("first displaytime sucess")
	local timertxt = Instance.new("TextBox", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top)
	timertxt.Parent = timer
	timertxt.Size = UDim2.new(0,50,0,50)
	timertxt.TextScaled = true
	timertxt.TextColor3 = Color3.new(255,255,255)
	timertxt.BackgroundColor3 = Color3.new(0,0,0)
	timertxt.BorderColor3 = Color3.new(0,0,0)
	timertxt.BackgroundTransparency = 1



	local bincountdown = countdowntime
	repeat
		timertxt.Text = tostring(bincountdown)
		bincountdown = bincountdown - 1
		wait(1)
	until
	bincountdown < 1


	obstruction.Position = Vector3.new(-1.1, 0.9, 345.1)
	timer.Enabled = false
		buttonactive = false
		
		for i,v in pairs(game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"]:GetChildren()) do
			if v:IsA("Part") then  ----checking if its a part
				v.CanCollide = false
				v.Transparency = 1
	

	script.Parent.BrickColor = BrickColor.new("Dark green")
end

local function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then 
		if buttonactive == false then
			buttonactive = true
			displaytime(5) 
		else 
			print ("Error - Already active!")
		end
	end
	end
	
script.Parent.Touched:Connect(onTouch)

				end
			end
		end
	end

But now nothing works, the blocks start visible. The Button doesn’t activate. How do i fix this?
Also there are no errors appearing

for i, #game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"]:GetChildren() do
local Part = Blocks["Part"..tostring(i)]
    Part.CanCollide = true
    game:GetService("TweenService"):Create(Part, TweenInfo.new(0.3), {Transparency = 0}):Play()
end

Would do it

Is the “#” a typo? Its giving me an error “undefined variable”

No

To test it try this. Its probably a bug with something you did
local Folders = workspace.Model:GetChildren()

for i = 1, #workspace.Model:GetChildren() do
    print(tostring(i))
end

image

You should get this
image

When I put this in a script by itself it works, but placing it into the main script means it doesn’t work

Can I see the main script with it?

Oh, I just changed it to use a table.

script.Parent.BrickColor = BrickColor.new("Dark green")

local myTab = {game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"]:GetChildren()} --Finds Blocks

startPosition = Vector3.new(-1.1, 0.9, 345.1) --Coords of button top


local Button = game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top --Change Level number for each level
print("button found")
local buttonactive = false

function displaytime(countdowntime)
	print("function started")
	local timer = Instance.new ("BillboardGui", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top) --Timer Set
	timer.Size = UDim2.new(0,50,0,50)
	timer.StudsOffset = Vector3.new(0,5,0)
	timer.AlwaysOnTop = true
	timer.Enabled = true
	local obstruction = Button
	obstruction.Position = Vector3.new(-1.1, 0.4, 345.1) -- coords of button top but,(y-0.5)
	script.Parent.BrickColor = BrickColor.new("Really red")
	print("upto new part")
	
	for i,v in pairs(myTab) do --Blocks Appearing
		v.CanCollide = true
		v.Transparency = 0
	

	print("first displaytime sucess")
	local timertxt = Instance.new("TextBox", game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Button"].Button.Top) --Text Timer Made
	timertxt.Parent = timer
	timertxt.Size = UDim2.new(0,50,0,50)
	timertxt.TextScaled = true
	timertxt.TextColor3 = Color3.new(255,255,255)
	timertxt.BackgroundColor3 = Color3.new(0,0,0)
	timertxt.BorderColor3 = Color3.new(0,0,0)
	timertxt.BackgroundTransparency = 1



	local bincountdown = countdowntime --TimeKeeper
	repeat
		timertxt.Text = tostring(bincountdown)
		bincountdown = bincountdown - 1
		wait(1)
	until
	bincountdown < 1


	obstruction.Position = Vector3.new(-1.1, 0.9, 345.1) --Button popping up after timer
	timer.Enabled = false
	buttonactive = false
		
	for i,v in pairs(myTab) do --Blocks Dissapearing
		v.CanCollide = false
		v.Transparency = 1
	

script.Parent.BrickColor = BrickColor.new("Dark green")


local function onTouch(hit) --Timer starts
	if hit.Parent:FindFirstChild("Humanoid") then 
		if buttonactive == false then
			buttonactive = true
			displaytime(5) --Timer Length
		else 
			print ("Error - Already active!")
					end
		script.Parent.Touched:Connect(onTouch)
				end
			end	
		end
	end
end 

I tried using the table as it seemed easier, I don’t know if I set it up correctly though. I think the issue the function not firing

local Blocks = {game.Workspace["Obby Structure"]["Levels 1-10"]["7"]["Blocks"]:GetChildren()}

Is this how I should take it out of the table?
if you haven’t already caught on, I have no idea what I’m doing

A table is the {} so remove the 2 {}