Help improving my code

Hey everyone! I’m trying to animate a light switch in a house model, so I wrote this code:

local isOn = true
local folder = script.Parent.Parent.Parent.Parent.Parent.Lights
local light1 = folder.OFFICE.OfficeLight01.Lamp.Light

function on()
	isOn = true
	light1.Parent.Material = Enum.Material.Neon
	light1.Enabled = true
	script.Parent.Orientation = Vector3.new(0, 100, 10)
end

function off()
	isOn = false
	light1.Parent.Material = Enum.Material.Glass
	light1.Enabled = false
	script.Parent.Orientation = Vector3.new(0, 100, 0)
end

function onClicked()
	script.Parent.Switch:Play()

	if isOn == true then off() else on() end

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

off()

It works well, but as you might guess, if I change the model orientation it happens:

this because I’m setting an orientation value instead of adding some value to it
script.Parent.Orientation = Vector3.new(0, 100, 0)

So, how could I make this code work better, so I don’t need to change the value everytime I change the rotation of the house? Any help will be highly appreciated!

Use += instead of =. .

1 Like

try this

local isOn = true
local folder = script.Parent.Parent.Parent.Parent.Parent.Lights
local light1 = folder.OFFICE.OfficeLight01.Lamp.Light

function on()
	isOn = true
	light1.Parent.Material = Enum.Material.Neon
	light1.Enabled = true
	script.Parent.Orientation = Vector3.new(0, 100, 0)
end

function off()
	isOn = false
	light1.Parent.Material = Enum.Material.Glass
	light1.Enabled = false
	script.Parent.Orientation = Vector3.new(0, 100, 10)
end

function onClicked()
	script.Parent.Switch:Play()

	if isOn == true then off() else on() end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

off()
1 Like

Honestly, I am wondering whether you might be better off making a simple transparency switch alternate between two parts that act as ‘On’ and ‘Off’. It saves you from calculating the orientation each time you want to duplicate the switch and use it elsewhere, and it accomplishes the same task.

1 Like

The solution is really simple. You have to set the Y direction of the main (Base part of the switch) in the switch orientation.

Script:

-- Bool value

local isOn = true -- Bool

-- Light

local folder = script.Parent.Parent.Parent.Parent.Lights -- Lights folder

local light1 = folder.Light.SurfaceLight -- Lights

-- Switch Parts:

local trig = script.Parent.Parent.Parent.trig -- Trigger

local main = script.Parent.Parent.Parent.Main -- Main part

-- Direction

local dirS = nil -- Y direction

dirS = main.Orientation.Y -- Set the Y direction of the main


-- Functions

function on()

isOn = true

light1.Parent.Material = Enum.Material.Neon

light1.Enabled = true

script.Parent.Parent.Parent.trig.Orientation = Vector3.new(0, dirS, 10)

end

function off()

isOn = false

light1.Parent.Material = Enum.Material.Glass

light1.Enabled = false

script.Parent.Parent.Parent.trig.Orientation = Vector3.new(0, dirS, -10)

end

function onClicked()

-- Other things

if isOn == true then off() else on() end

end

script.Parent.Parent.ClickDetector.MouseClick:connect(onClicked)

off()

here is the switch in the explorer:

image

and the lights folder, with its parts:

image


If you have multiple lights and the switch have to enable or disable all of them you need a for loop:

-- Bool value

local isOn = true -- Bool

-- Light

local folder = script.Parent.Parent.Parent.Parent.Lights -- Lights folder

local lights = folder:GetDescendants() -- Lights

-- Switch Parts:

local trig = script.Parent.Parent.Parent.trig -- Trigger

local main = script.Parent.Parent.Parent.Main -- Main part

-- Direction

local dirS = nil -- Y direction

dirS = main.Orientation.Y -- Set the Y direction of the main

-- Tween

local tween = game:GetService("TweenService") -- TweenService


-- Functions

function on()

	isOn = true

	for _,v in pairs(lights) do
		--[[ If you have both Surface light and Spot light you can easily add:
		if v:IsA("SurfaceLight") or v:IsA("SpotLight") then ]]--
		
		if v:IsA("SurfaceLight") then -- Checks if the object is a "Surface Light", and if it is true then:
			
			v.Parent.Material = Enum.Material.Neon -- Change material
			v.Enabled = true -- Enable the light
		end
	end
	
	script.Parent.Parent.Parent.trig.Orientation = Vector3.new(0, dirS, 10)
end

function off()
	-- Same think that i did above:

	isOn = false
	
		for _,v in pairs(lights) do
		if v:IsA("SurfaceLight") then
			v.Parent.Material = Enum.Material.Glass
			v.Enabled = false
		end
	end
	
	script.Parent.Parent.Parent.trig.Orientation = Vector3.new(0, dirS, -10)
end

function onClicked()
	-- Other things

	if isOn == true then off() else on() end 

end

-- Event

script.Parent.Parent.ClickDetector.MouseClick:connect(onClicked)

off()

I want to give you some advice on the aesthetics of switch. If you want to make more realistic the light switch, give it an animation:

-- Bool value

local isOn = true -- Bool

-- Light

local folder = script.Parent.Parent.Parent.Parent.Lights -- Lights folder

local lights = folder:GetDescendants() -- Lights

-- Switch Parts:

local trig = script.Parent.Parent.Parent.trig -- Trigger

local main = script.Parent.Parent.Parent.Main -- Main part

-- Direction

local dirS = nil -- Y direction

dirS = main.Orientation.Y -- Set the Y direction of the main

-- Tween

local tween = game:GetService("TweenService") -- TweenService


-- Functions

function on()

	isOn = true
	
	for _,v in pairs(lights) do
		--[[ If you have both Surface light and Spot light you can easily add:
		if v:IsA("SurfaceLight") or v:IsA("SpotLight") then ]]--
		
		if v:IsA("SurfaceLight") then -- Checks if the object is a "Surface Light", and if it is true then:
			
			v.Parent.Material = Enum.Material.Neon -- Change material
			v.Enabled = true -- Enable the light
		end
	end
	
	-- Create an animation for the trigger, you can customize it on what you like. I link to you the animation doc at the end of the reply.
	tween:Create(trig, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),{Orientation = Vector3.new(0, dirS, 10)}):Play()
end

function off()
	-- Same think that i did above:

	isOn = false
	
		for _,v in pairs(lights) do
		if v:IsA("SurfaceLight") then
			v.Parent.Material = Enum.Material.Glass
			v.Enabled = false
		end
	end
	
	tween:Create(trig, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),{Orientation = Vector3.new(0, dirS, -10)}):Play()
end

function onClicked()
	-- Other things

	if isOn == true then off() else on() end 

end

-- Event

script.Parent.Parent.ClickDetector.MouseClick:connect(onClicked)

off()

And for the end you can add a sound when you click on the trigger:

I advise you to local only one sound, and not a sound per trigger, this because is more efficient and customizable.

image


Here is the final script:

-- Bool value

local isOn = true -- Bool

-- Light

-- There i added a "Parent" to the local because we inserted the switch in a folder.
local folder = script.Parent.Parent.Parent.Parent.Parent.Lights -- Lights folder

local lights = folder:GetDescendants() -- Lights

-- Switch Parts:

local trig = script.Parent.Parent.Parent.trig -- Trigger

local main = script.Parent.Parent.Parent.Main -- Main part

-- Direction

local dirS = nil -- Y direction

dirS = main.Orientation.Y -- Set the Y direction of the main

-- Sound

local Click_Sound = main.Parent.Parent.Sound.Click_Sound -- The click sound

-- Tween

local tween = game:GetService("TweenService") -- TweenService


-- Functions

function on()

	isOn = true
	
	for _,v in pairs(lights) do
		--[[ If you have both Surface light and Spot light you can easily add:
		if v:IsA("SurfaceLight") or v:IsA("SpotLight") then ]]--
		
		if v:IsA("SurfaceLight") then -- Checks if the object is a "Surface Light", and if it is true then:
			
			v.Parent.Material = Enum.Material.Neon -- Change material
			v.Enabled = true -- Enable the light
		end
	end
	
	-- Create an animation for the trigger, you can customize it on what you like. I link to you the animation doc at the end of the reply.
	tween:Create(trig, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),{Orientation = Vector3.new(0, dirS, 10)}):Play()
end

function off()
	-- Same think that i did above:

	isOn = false
	
		for _,v in pairs(lights) do
		if v:IsA("SurfaceLight") then
			v.Parent.Material = Enum.Material.Glass
			v.Enabled = false
		end
	end
	
	tween:Create(trig, TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut),{Orientation = Vector3.new(0, dirS, -10)}):Play()
end

function onClicked()
	
	-- Play click sound:
	Click_Sound:Play()

	if isOn == true then off() else on() end 

end

-- Event

script.Parent.Parent.ClickDetector.MouseClick:connect(onClicked)

off()

Documentation:


I hope this helped!

1 Like