Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Lamp turns on but can't be turned off?

Code I have so far:

local OriginalPos = Vector3.new(5.497, 0.312, -7.312)
script.Parent.ClickDetector.MouseClick:Connect(function()
	script.Parent.Position = Vector3.new(5.497, 0.248, -7.312)
	wait(0.25)
	script.Parent.Position = OriginalPos
	script.Parent.Parent.Light.PointLight.Enabled = true
	script.Parent.Parent.Light.Material = Enum.Material.Neon
end)

script.Parent.ClickDetector.MouseClick:Connect(function()
	script.Parent.Position = Vector3.new(5.497, 0.248, -7.312)
	wait(0.25)
	script.Parent.Position = OriginalPos
	script.Parent.Parent.Light.PointLight.Enabled = false
	script.Parent.Parent.Light.Material = Enum.Material.Plastic	
end)

So when I press the button, the lamp turns on (PointLight enables) and the Material changes to neon. But I press it again and then it stays on and it's still neon! The button moves, but nothing turns off when it's clicked.

3 comments

0

Use a debounce

0

i didnt think it would use a debounce but ill try it

0

you can just use the not operator: script.Parent.Parent.Light.PointLight.Enabled = not script.Parent.Parent.Light.PointLight.Enabled

1 answer

you put 2 function so it will run in the same times. Make a variable to check.

local lampOn = false --Here

local OriginalPos = Vector3.new(5.497, 0.312, -7.312)
script.Parent.ClickDetector.MouseClick:Connect(function()
    if lampOn == false then -- Check
        lampOn = true
        script.Parent.Position = Vector3.new(5.497, 0.248, -7.312)
        wait(0.25)
        script.Parent.Position = OriginalPos
        script.Parent.Parent.Light.PointLight.Enabled = true
        script.Parent.Parent.Light.Material = Enum.Material.Neon
    else
        lampOn = false
        script.Parent.Position = Vector3.new(5.497, 0.248, -7.312)
        wait(0.25)
        script.Parent.Position = OriginalPos
        script.Parent.Parent.Light.PointLight.Enabled = false
        script.Parent.Parent.Light.Material = Enum.Material.Plastic
    end
end)

1 comment

0

make your comments more descriptive lol

This discussion is preserved as it was. New answers, comments, and votes are closed.