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

Problem with buttons?

Asked by 8 years ago

Script:

local Lights = script.Parent.Lights
local a = Lights.A
local b = Lights.B
local c = Lights.C
local d = Lights.D
local e = Lights.E
local f = Lights.F
local g = Lights.G
local h = Lights.H
local i = Lights.I
local j = Lights.J
local k = Lights.K
local l = Lights.L
local Check = script.Parent.ButtonG.Button.Check.Value

function Rotate() -- Light Rotation
    a.Material = "Neon"
    wait(0.1)
    a.Material = "SmoothPlastic"
    b.Material = "Neon"
    wait(0.1)
    b.Material = "SmoothPlastic"
    c.Material = "Neon"
    wait(0.1)
    c.Material = "SmoothPlastic"
    d.Material = "Neon"
    wait(0.1)
    d.Material = "SmoothPlastic"
    e.Material = "Neon"
    wait(0.1)
    e.Material = "SmoothPlastic"
    f.Material = "Neon"
    wait(0.1)
    f.Material = "SmoothPlastic"
    g.Material = "Neon"
    wait(0.1)
    g.Material = "SmoothPlastic"
    h.Material = "Neon"
    wait(0.1)
    h.Material = "SmoothPlastic"
    i.Material = "Neon"
    wait(0.1)
    i.Material = "SmoothPlastic"
    j.Material = "Neon"
    wait(0.1)
    j.Material = "SmoothPlastic"
    k.Material = "Neon"
    wait(0.1)
    k.Material = "SmoothPlastic"
    l.Material = "Neon"
    wait(0.1)
    l.Material = "SmoothPlastic"
end

function OnClicked(playerWhoClicked) -- When button is clicked
    wait(1)
    Check = 1 -- 0 = Inactive and 1 = Active, thus making the game active
    while Check == 1 do
        Rotate()
        wait(0.1)

    end
    if script.Parent.ButtonS.Button.ClickDetector.MouseClick then
        Check = 0
    end
end

script.Parent.ButtonG.Button.ClickDetector.MouseClick:connect(OnClicked)

Problem: 1: The script does not detect the click of the second button, thus unable to end. 2: The first button is able to be repeatedly activated.

If there's a section that can be simplified, please tell me. Cheers.

1 answer

Log in to vote
1
Answered by 8 years ago

I have read the code and I believe I can identify where the issues you are facing come from.

Problem 1: If I am correct, you are checking for a mouse click on lines 63 to 65, which will set the check variable to 0 and thus end the loop. In the code above though, the if statement will only ever fire AFTER the loop has already ended, as it is nested outside of the loop itself. You can fix this by moving lines 63-65 to the current empty space on line 61 (after your wait statement)

Problem 2: Set a variable in the upper most scope of your script (where you have your lights variables) as a boolean, such as "buttonInUse" and once the button has been pressed, set it to true, and once it passes the check, set it to false. When the button event is pressed, check that "buttonInUse" is not true.

As for simplification, programming is about automation and doing things in an efficient and clean mannor. Whilst this is true, I do not believe it is needed in ROBLOX unless your creation will complex and require clean code in hopes of being ever completed.

But for pointers:

Your lights seem to all be kept in an group called "Lights", which is a good design choice! But you've not capitalised on this by writing out each light variable seperately and accessing them via code seperately.

You could do as follows:

local lightList = Lights:GetChildren() -- Gets a list of all of the children in the Lights group
for i = 1, #lightList do
    if(i = 1)then -- We only want to set a material at the first step
        lightList[i].Material = "Neon"
    else -- We now want to remove the last, and set the current
        lightList[i - 1].Material = "Smooth Plastic"
        lightList[i].Material = "Neon"
    end 
    wait(0.1)
end
Ad

Answer this question