How can i make this code more efficient or clean

I’m trying to make where 3 of the ProximityPrompt buttons are pressed, then a part(door) will open. It worked as i expected but is there anyway to make this script more efficient? looking for more scripting knowledge :wink:

local door = game.Workspace.Door

local button1 = game.Workspace.Button.ProximityPrompt
local button2 = game.Workspace.Button2.ProximityPrompt2
local button3 = game.Workspace.Button3.ProximityPrompt3

local Clicked1 = false
local Clicked2 = false
local Clicked3 = false

button1.Triggered:Connect(function()
	Clicked1 = true
	print("1, true")
end)

button2.Triggered:Connect(function()
	Clicked2 = true
	print("2, true")
end)

button3.Triggered:Connect(function()
	Clicked3 = true
	print("3,true")
end)

while true do
	if Clicked1 and Clicked2 and Clicked3 == true then
		print("all true")
		door.Transparency = 1
		door.CanCollide = false
		wait(5)
		door.Transparency = 0
		door.CanCollide = true
		wait(5)
	end
	wait(1)
end
3 Likes

You can use functions

local door = game.Workspace.Door

local button1 = game.Workspace.Button.ProximityPrompt
local button2 = game.Workspace.Button2.ProximityPrompt2
local button3 = game.Workspace.Button3.ProximityPrompt3

local Clicked1 = false
local Clicked2 = false
local Clicked3 = false

function onButton1Triggered()
   Clicked1 = true
   print("1, true")
end

button1.Triggered:Connect(onButton1Triggered)
2 Likes

where do i put the last line at?

Functions are just code variables (I don’t know how do I explain them). You can insert the last line at the last line.

1 Like

Also forgot to tell, you can use ModuleScripts too for storing functions so you get less lines of code.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.