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

How do I restart a part of a function without remaking it?

The code below is just trying to turn on the SpotLight placed in part but the issue I have is after activating the light and clicking the part once again to turn it off, the script refuses to run the function and does not return any errors in output.

light = script.Parent.SpotLight
charge = script.Parent.Charge.Value
button = script.Parent.ClickDetector

function enable()
	if charge >=  100 then
	light.Enabled = true
	charge = (charge - 20)
	print ("on")
	print (charge)
	elseif light.Enabled == true then
		print ("Already on, turning off")
		wait (5)
		light.Enabled = false
	end
end
	
script.Parent.ClickDetector.MouseClick:connect(enable)

3 comments

0

Have you checked your charge variable after clicking it the first time?

0

Right I just noticed that but I've just lowered the value at line 6 but now the issue is that when I click again, instead of turning off after 5 seconds, it just acts as if the part starting at the elseif doesn't exist

0

Posted a solution based off yours and Ruben's input. Thanks for the assist.

2 answers

✓ Accepted answer

What happens in your script is that the code will go from top to bottom. It will encounter the first if case: Charge >= 100. if this is true, it will not look to any other cases and go about executing the code in the case. so to fix your code, you will have to make the case like this: if charge >= 100 and not light.Enabled == true then. this way it will check if the value of your charge variable, if this is true (e.g. exceeds or is 100) it will also look if the lamp is not already turned on, if this is not the case it will continue with the code in the case, otherwise it will go to the next case which is elseif and test the requirements for that. hope it helps!

1 comment

0

Yeah I added a debounce and relocated and added if/elseif statements. So it works correctly plus some. Posting my updated code as an answer so if you would take a look that would be great.

Updated and FIXED code

light = script.Parent.SpotLight
charge = script.Parent.Charge.Value
button = script.Parent.ClickDetector
local isOn = false

function enable()
	if isOn == true then
		isOn = false
		print("Already on, turning off")
		print(charge)
		wait(0.5)
		light.Enabled = false
	elseif charge == 0 then
		isOn = false
		print("No charge")
		light.Enabled = false
	elseif charge >= 20 then
		isOn = true
		charge = (charge - 20)
		print("Turning on")
		wait(0.01)
		light.Enabled = true
		print(charge)
	end
end

script.Parent.ClickDetector.MouseClick:connect(enable)

1 comment

0

this is also a valid solution! good job! you might even delete the lines Light.Enabled = false in the charge == 0 case, because the state will not be reached if the light is on. Beware: there is no statement for the case when a player has a charge of 1, this could be solved by making the before mentioned case (charge == 0 ) to charge < 20

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