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

If then Elseif Problems?

Asked by 10 years ago

So I'm making a game with classes, so I made a localscript with a few lines making BoolValues of each class. And that works because when I inspect player, there are all of the classes.

So I already made the script to give you the class items, and that works too.

The problem I have now Is trying to make a script that prevents you from re-clicking the Gui to change to that class if you're already that class.

Here's the script:

textButton = script.Parent
spawns = {game.Workspace.SpawnLocation1 , game.Workspace.SpawnLocation2 , game.Workspace.SpawnLocation3 , game.Workspace.SpawnLocation4}
player = script.Parent.Parent.Parent.Parent
Trowel = game.ReplicatedStorage.ClassicTrowel:Clone() 
Builder = player:WaitForChild("Builder")




textButton.MouseButton1Down:connect(function()
    if Builder.Value == false then
    Builder.Value = true
    script.Parent.Text = "5"
    wait(1)
    script.Parent.Text = "4"
    wait(1)
    script.Parent.Text = "3"
    wait(1)
    script.Parent.Text = "2"
    wait(1)
    script.Parent.Text = "1"
    wait(1)
    player.Character:MoveTo(spawns[math.random(1, #spawns)].Position)
    player.Character.Humanoid.MaxHealth = 200
    player.Character.Humanoid.Health = 200
    Trowel:Clone().Parent = player.Backpack 
    Trowel:Clone().Parent = player.StarterGear
    script.Parent.Text = "Change to Builder"
    end
    end)
else
    return
    end

I know there's an error, and it's been annoying me. It says:

'<eof>' expected near 'else'

I know that means that there should be whitespace where that is, but that completely erases the else part.

I have trouble with these conditional statements and where to place my 'ends'

0
The 'else' and 'elseif' type methods are used inside of a 'if then' statement, your 'else' is outside of one looking at your code. TheeDeathCaster 2368 — 10y

2 answers

Log in to vote
0
Answered by 10 years ago

Heres a small code that's fairly simple... I'm fairly good at this kind of coding due to I made a lot of Admin's and yea you kinda have to get good at If and else statements or the scripts breaks ;-;

data=false
data2=true
if data==true then
print('data 1')
elseif data2==true then
print('data2')
else 
print('hai')
end
0
I know how to use those terms, but with more code like mine I get confused, but thanks for trying to help SpazzMan502 133 — 10y
Ad
Log in to vote
0
Answered by
nilVector 812 Moderation Voter
10 years ago

Your problem was that you ended your if statement, and then you ended your function, and then you put your else statement.

if's and elseif's/else's always share the same end.

This is what it's supposed to look like:

textButton.MouseButton1Down:connect(function()
    if Builder.Value == false then
        Builder.Value = true
        script.Parent.Text = "5"
        wait(1)
        script.Parent.Text = "4"
        wait(1)
        script.Parent.Text = "3"
        wait(1)
        script.Parent.Text = "2"
        wait(1)
        script.Parent.Text = "1"
        wait(1)
        player.Character:MoveTo(spawns[math.random(1, #spawns)].Position)
        player.Character.Humanoid.MaxHealth = 200
        player.Character.Humanoid.Health = 200
        Trowel:Clone().Parent = player.Backpack
        Trowel:Clone().Parent = player.StarterGear
        script.Parent.Text = "Change to Builder"
    else
        return
    end
end)

Try to use proper indentation and syntax so that you don't encounter these errors as often. It makes it easier for you and others to read your code and find mistakes.

Answer this question