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

Why wont my on/off function work?? It just errors pls help (it's a Gui localscript)

Asked by 4 years ago

so it errors with this... Players.Deinonychusaurus.PlayerGui.BreadGui.TextButton.LocalScript:17: Expected 'end' (to close 'else' at line 11), got <eof> code :

local player = game:GetService("Players").LocalPlayer
local button = script.Parent 
local Digfunction = player.Backpack.DigFunction
local xd = true

button.Activated:Connect(function()

    if xd == true then
    Digfunction.Disabled = true
    xd = false
    else
    if xd == false then
    Digfunction.Disabled = false
    xd = true


end

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

At line 10, you set XD to false. And at line 12, you wanted to set XD to true if XD was false. The problem is that line 12 is after line 10. So it will always set XD to true. We have to find a way to return to the beginning of the function to not run the lines 12 to 14 just after setting XD to false. We can us the function return to return to the beginning of the function:

local xd = false

Button.Activated:Connect(function()
    if xd == false then
            xd = true
            DigFunction.Disabled = true
            return
    end

    if xd == true then
           xd = false
           DigFunction.Disabled = false
           -- we don't need to return, cuz it's the end of the function.
    end
end)

Also, idk why you added a else at line 11. If you wanted to use the else function, delete line 12.

Hope this helped!

Sorry I didn't have time to write all the variables cuz I'm on mobile. Sorry.

Ad
Log in to vote
-1
Answered by 4 years ago
Edited 3 years ago

I think this is what you wanted

local player = game:GetService("Players").LocalPlayer
local button = script.Parent 
local Digfunction = player.Backpack.DigFunction
local xd = true

button.Activated:Connect(function()

    if xd == true then
    Digfunction.Disabled = true
    xd = false
    elseif xd == false then -- this was a bad boy
    Digfunction.Disabled = false
    xd = true
end

end
0
Oh thanks! Deinonychusaurus 21 — 4y

Answer this question