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:
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.
Here is a fixed version, which (Now, because I fixed it ) 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)