Light switch not working

Hello DevForum Community!

I am adama88766, and I just started scripting Lua. I am super new to it. (Less than 3 weeks)

I have one of my first scripts that I have made, obviously using a tutorial, but I have a hunch it has to do with calling the part.

light = script.Parent.Parent.Parent.Parent.LightBox -- This is the light bulb
switch = script.Parent.Parent -- this is the switch

function onClick()
if light.PointLight.Range == 10 then
	light.PointLight.Range = 0
else
	light.PointLight.Range = 10 --how bright you want it to be
end
end

script.Parent.mouseClick:connect(onClick)

I want this script to work even if the lights are in a folder or a different destination.

Basically, I need knowledge on how to do the first 2 lines of this script. How to do the location of the light, and the switch.

As I said, I am a super newb at this…

2 Likes

I also do not get these as aren’t you suppose to click the switch to turn on the light?

switch.ClickDetector.mouseClick:Connect(onClick)
2 Likes

So I can’t provide a screenshot, which would have been more helpful, but I can word it out.

So in the workspace, with a lot of other stuff, there is a folder called, “House”. In that folder there is another called “Garage”. In the folder “Garage”, there is another folder called “GarageLights”. And in that folder, “GarageLights” there is a model called “LightSwitch” With the script and light switch button, and all of the light switch parts, and there are 6 parts called “LightBoxes” with a pointlight and a frame for the actual light.

The script works if I put the model “LightSwitch” and all of the “LightBoxes” in the actual workspace put doesn’t work if I put it in a folder, or through multiple like above.

I did use a online tutorial fyi.

1 Like

instead of doing light.PointLight.Range = 0/10, you can just set the enabled property of it to true/false

function onClick()
if light.PointLight.Enabled == false then
	light.PointLight.Enabled = true
else
	light.PointLight.Range = false
end
end

if you need additional help on PointLights, go to this link here: https://developer.roblox.com/en-us/api-reference/class/PointLight

1 Like

like this?

light = script.Parent.Parent.Parent.Parent.LightBox -- This is the light bulb
switch = script.Parent.Parent -- this is the switch

function onClick()
if light.PointLight.Enabled == false then
light.PointLight.Enabled = true
else
light.PointLight.Range = false
end
end

script.Parent.mouseClick:connect(onClick)

EDIT: The light script works, mine and yours, but the real problem is my previous reply. How to locate the light switch and the light boxes in folders.

1 Like

yes.

30 character limit

1 Like
If you still need help then open this

I will just tell you what to do as this is simple.

Make a model and put two parts inside. One called “Switch” the other called “Light”.
Put a click detector and a script inside switch.
In the script type:

local light = script.Parent.Parent.Light.PointLight
local switch = script.Parent

switch.ClickDetector.MouseClick:Connect(function()
	if light.Enabled == true then
		light.Enabled = false
	else
		light.Enabled = true
	end
end)

Then put a PointLight inside “Light” and done.

2 Likes

It is wrong

not range it is “Enabled”

1 Like

I got that down. But if I put the “Light” and the "Switch in a folder and put them in different models, what should I do?

1 Like

I mean it they both have the same parent then leave it.

1 Like

No, the 2 first lines.

local light = script.Parent.Parent.Light.PointLight
local switch = script.Parent

EDIT: Where the light and switch are in the workspace, I think that’s what they are for.

1 Like

Yes, read my edited post above.
If they both have the same parent then leave it but if they have diffren parens then write a new variable for it.

1 Like
local folder = script.Parent.Folder --change "Folder" to whatever the folder's name is
local light = folder:WaitForChild("PointLight")
local switch = folder:WaitForChild("Switch")
1 Like

They have the same parent and the same parent is

So the parent would be the folder, “GarageLights”?

1 Like

Okay lets just say.

We put the light block and the switch block inside the same group.
If that is the case then leave it like i showed you.

If they are in diffrent groups or just the yhave diffrent parents then write a new variable for it.

1 Like

So this is my current script.

light = script.Parent.Parent.Parent.Parent.LightBox -- This is the light bulb

switch = script.Parent.Parent -- this is the switch

function onClick()

if light.PointLight.Enabled == false then

light.PointLight.Enabled = true

else

light.PointLight.Enabled = false

end

end

script.Parent.mouseClick:connect(onClick)
1 Like

It is correct
If you really don’t get it check this model i made

1 Like

They aren’t in a group, the model “LightSwitch” is a model with the light parts, the frame, screws, etc. and in that model there is a part called “switch” with a click detector and the script inside of the click detector. Then for the actual lights the switch is wired to, it is a part, not a model, with a pointlight and another part for a frame.

My actual question is, they work if they are in the workspace, but not if they are in the same folder, and that same folder is in 2 different folders.

1 Like

Can you show us the explorer?

What instance is child of other instance.

1 Like

If you are in studio,just take a screenshot of the tree.

1 Like