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

Where to place 'end'?

Asked by 10 years ago

This has been bothering me a lot, So I posted earlier about a script that decreases a Gui size, but I'm having trouble finding a location where to put this in.

At the moment there is no end, and it says 17:37:17.653 - Workspace.Water2.WaterScript:87: 'end' expected (to close 'function' at line 8) near '<eof>'

I have tried putting it after every function, and it either stops the script right there, or doesn't even allow the script to work.

It could be the placement of the Gui decrease script (lines 34 - 50)

This is the script

local Tool = script.Parent;

enabled = true




function onActivated()
    if not enabled  then
        return
    end



    enabled = false
    Tool.GripForward = Vector3.new(0,-.759,-.651)
    Tool.GripPos = Vector3.new(1.5,-.5,.3)
    Tool.GripRight = Vector3.new(1,0,0)
    Tool.GripUp = Vector3.new(0,.651,-.759)


    Tool.Handle.DrinkSound:Play()
    script.Parent.Bites.Value = script.Parent.Bites.Value + 1
Eable()
    wait(3)


    local character = Tool.Parent
    local player = game.Players:GetPlayerFromCharacter( character )

    local thirst = player.PlayerGui.Thirst


    local maxthirst = 100 --create a max thirst value
local currentthirst = 100 --create a currentthirst value

local currentthirstpercent = currentthirst/maxthirst --divide currentthirst by maxthirst to create a value between 0 and 1 to represent the percentage of thirst left.

function updatebar() --a function that will update the thirst bar. run this whenever you change the thirst
    local currentthirstpercent = currentthirst/maxthirst --re-divides the thirst values
    thirst.BG.Bar.Size = UDim2.new(currentthirstpercent,0,1,0) --sets the size in the x of the bar to the percentage of thirst remaining
end

function drink() --example function for increasing thirst
    currentthirst = currentthirst + -50 --add thirst together
    if currentthirst > maxthirst then --checks to see you aren't over the max
        currentthirst = maxthirst --if you're over the max it sets you to the max
    end
    function updatebar() --updates the bar because you changed the value of thirst
end






    Tool.GripForward = Vector3.new(-.976,0,-0.217)
    Tool.GripPos = Vector3.new(0.03,0,0)
    Tool.GripRight = Vector3.new(.217,0,-.976)
    Tool.GripUp = Vector3.new(0,1,0)




enabled = true
end


function onEquipped()
    Tool.Handle.OpenSound:play()
end

function Eable()
if (script.Parent.Bites.Value) > 5 then 
script.Parent:Remove()
end
end




script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)

3 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

This isn't a specific answer to your code, but a general answer about tabbing and pairing ends with things.

Tabbing and ends

Tabbing code correctly is very important -- but first you have to know what tabbing correctly looks like!

In Lua, we have lines of code. Usually a line will be a single statement:

wait(5)
local part = game.Workspace.Part

They will also be controls, ends, and function definitions:

if condition then
elseif blah then
else
end
function doSomething()
end

Lua itself doesn't care where you put lines (or tabs, or spaces). Thus, when we say "line", we mean the way that it's usually done (like above).


There are also blocks of code. These are groupings of lines inside a particular control structure:

  • Between then and any of else, elseif, end
  • Between else and end
  • Between do and end (for do, while do, and also just do)
  • Between repeat and until
  • Between function and end

Each subsequent block gets tabbed:

function doSomething( arg, dog )
    if not arg then
        for i = 1, 20 do
            print(i)
        end
    elseif arg + 9 == 0 then
        if dog == "cat" then
            do
                print("It's a ", dog)
            end
        end
    end

    function insideOtherFunction() -- don't usually do this
        print("Something!")
    end
    insideOtherFunction()
end

The basic rules:

do, then, function, else start blocks. end and until stop them.

Every time you start one, you increase number of tabs. Every time you stop one, you decrease number of tabs.

Then everything "inside" should be to the right of the thing.

Thus if you see something like

            ....
            if blah then
                start()
                middle()
                finish()
            after()
            always()
        end

Something is wrong -- indentation decreased when there wasn't an end, so you probably made a mistake in placing the end (it should probably have gone between finish() and after())

Ad
Log in to vote
0
Answered by 10 years ago

At line 49, you attempted to call the function incorrectly. You said function updatebar() instead of just updatebar(). Hope this helped!

Log in to vote
0
Answered by
Ryzox 220 Moderation Voter
10 years ago

Well it doesn't help that your tabbing is messy, once you can get your tabbing better you will find it a lot easier to put in ends/else/elseifs.

I suggest tabbing it all out and checking if there are any areas missing an end.

And you are attempted to overwrite your function updatebar() in line 49 inside another function, I suggest taking a look at that.

1
I personally don't know how to tab correctly, can someone give me a brief overview of tabbing? SpazzMan502 133 — 10y
1
Every time there is like a statement you know the blue bold letters like if, function, while, stuff like that, you tab the line underneath it and then put the end with no tab so it should be in line with your statement you made and it should be easier to see where it starts and finishes Ryzox 220 — 10y

Answer this question