Need help with my script

So i was making a simple streetlight:

and i tried to make it togglable on/off, i pointed out where on and off buttons should be in the screenshot, but no matter what i do, it just won’t work.
Here’s the model composition of said streetlight: Screenshot_163
And here’s the code itself:

local on = script.Parent
local lamp = on.Parent.Light
local off = on.Parent.Off
on.ClickDetector.MouseClick:Connect (function(player)
	off.ClickDetector.MouseClick:Connect (function(player)
		local function UpdateVisuals()
			if
			on.ClickDetector.MouseClick
			then
			wait (0.1)
			lamp.Material = Enum.Material.Neon
			Instance.new ("PointLight",lamp)
			elseif
				off.ClickDetector.MouseClick
				then
				lamp.PointLight Destroy()
				lamp.Material = Enum.Material.Plastic
		end
	end
	
end)

Also, i tried to aim for it to call objects only from inside the model.
If someone could tell me what i’m doing wrong, and lead me the right way, it would be greatly appreciated, as this is one of the most complex code blocks i’ve ever written myself.

1 Like

I think you should separate the functions of MouseClick. Because the code breaks is you use 2 functions one inside other

Here is a fixed version, which (Now, because I fixed it :sweat_smile: ) works:

local on = script.Parent
local lamp = on.Parent.Light
local off = on.Parent.Off -- Dont think you refferenced it correctly :(

-- Update function

local function UpdateVisuals(Bool)
	if Bool == true then
		wait (0.1)
		lamp.Material = Enum.Material.Neon
		Instance.new ("PointLight",lamp)
	elseif Bool == false then
		lamp:FindFirstChild("PointLight"):Destroy()
		lamp.Material = Enum.Material.Plastic
	end
end

-- Events --
on.ClickDetector.MouseClick:Connect (function() UpdateVisuals(true) end)
off.ClickDetector.MouseClick:Connect (function() UpdateVisuals(false) end)

Based on that, i split the code into 2 seperate ones,
one tied to On:

local on = script.Parent
local lamp = on.Parent.Light
on.ClickDetector.MouseClick:Connect (function(player)
		local function UpdateVisuals()
			if
			on.ClickDetector.MouseClick
			then
			wait (0.1)
			lamp.Material = Enum.Material.Neon
			Instance.new ("PointLight",lamp)
		end
	end
	
end)

And one tied to Off:

local off = script.Parent
local lamp = script.Parent.Light
on.ClickDetector.MouseClick:Connect (function(player)
	local function UpdateVisuals()
		if
			on.ClickDetector.MouseClick
		then
			wait (0.1)
			lamp.Material = Enum.Material.Plastic
			lamp.PointLight = Destroy()
		end
end)
local LightPart = script.Parent.Parent.Light
local Light = Instance.new("PointLight", LightPart)
Light.Enabled = false
Light.Brightness = 1
Light.Range = 8
Light.Shadows = false
Light.Color = Color3.fromRGB(255, 255, 255)
local On = script.Parent
local Off = script.Parent.Parent.Off


Off.ClickDetector.MouseClick:Connect(function()
	Light.Enabled = false
	LightPart.Material = Enum.Material.SmoothPlastic
end)


On.ClickDetector.MouseClick:Connect(function()
	Light.Enabled = true
	LightPart.Material = Enum.Material.Neon
end)
1 Like

I tested it out, it doesen’t work

Any error messages? What does it say in the output?

I tested it out, it works as it should, thank you, and have your reply marked as solution!

As for answer to your question, output literally says nothing about your script