I'm completely stuck please help {Easy / Intermediate}

  1. The goal is to try to make the script I’m writing light up a button if its on, and if its off turn it off.

  2. I can’t figure out the issue it literally feels like it’s just skipping everything and going straight to the else statement.

  3. I have checked if the button Material is set to Smoothplastic before I run it. I also know that having both the On and Off variables is a bit of a waste.

Here is the code

local Off = script.Parent.Material       -- SmoothPlastic
local Debounce = false


function Work()
	
	if Off == "SmoothPlastic" and Debounce == false then
		Debounce = true
		On = "Neon"
		wait(0.5)
		Debounce = false
	elseif On == "Neon" and Debounce == false then
		Debounce =  true
		Off = "SmoothPlastic"
		wait(0.5)
		Debounce = false
	else
		print("Bro whyyyyyyy just work;-;")
	end 
end



script.Parent.ClickDetector.MouseClick:Connect(Work)

Hopefully, it’s not a little error I have been overlooking this whole time o-o
Edit: On and Off are the same variable it combined them when I posted.

1 Like

Well, in the start of your code you make local Off = script.Parent.Material and then in your function, it checks if Off == "SmoothPlastic" and/or On == "Neon", which isn’t possible because script.Parent.Material will be an Enum.Material

Furthermore, your use of variables is very confusing as you have an On variable and an Off variable which don’t determine the part’s material. A correct version of code would be:

local on = false
local debounce = false
local onMaterial = Enum.Material.Neon
local offMaterial = Enum.Material.SmoothPlastic

function Work()
  if on and debounce == false then -- The light is on, you're switching it off
    script.Parent.Material = offMaterial
    on = false
    debounce = true
    wait(0.5)
    debounce = false
  elseif not on and debounce == false then -- The light is not on, you're switching it on
    script.Parent.Material = onMaterial
    on = true
    debounce = true
    wait(0.5)
    debounce = false
  end
end

script.Parent.ClickDetector.MouseClick:Connect(Work)

Hope this works.
Good luck.

1 Like

Thanks so much for taking the time to make a whole script. Now i have to go re-learn what Enum is lol.