Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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)
0
Have you checked your charge variable after clicking it the first time? GetGlobals 343 — 5y
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 PhysEngine 7 — 5y
0
Posted a solution based off yours and Ruben's input. Thanks for the assist. PhysEngine 7 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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!

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. PhysEngine 7 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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)
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`` Rubenske1 89 — 5y

Answer this question