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

Why is the if statement acting strange?

Asked by
Nidoxs 190
9 years ago

So I have an if else and elseif all after one another like this:

function lever(click)
local cn = click.Name
local b = game.ServerStorage.Script:Clone()
local a = Instance.new("BoolValue", game.Players[cn].PlayerGui)
a.Name = "Value"
a.Value = false
if click.Name == "doctorbenny2011" or click.Name ==  "CaptainTheTimeLord" or click.Name == "Player1" or click.Name == "Player" then
    if game.Players[cn].PlayerGui.Value.Value == false then -- A value so the gui only shows once.
    b.Parent = game.Players[cn].PlayerGui.Notify.Text   
    b.Disabled = false  
    a.Value = true  
    game.Players[cn].PlayerGui.Notify.Text.Visible = true
    game.Players[cn].PlayerGui.Notify.Text.BackgroundTransparency = .8
    game.Players[cn].PlayerGui.Notify.Text.TextTransprency = 0
    game.Players[cn].PlayerGui.Notify.Text.Text = "YOU are authorized."
    game.Players[cn].PlayerGui.Notify.Sound:Play()
else
--codey stuff 
elseif click.Name ~= "doctorbenny2011" or click.Name ~= "CaptainTheTimeLord" or click.Name ~= "Player" or click.Name ~= "Player1" then 
    print(click.Name)
    b.Parent = game.Players[cn].PlayerGui.Notify.Text
    b.Disabled = false
    game.Players[cn].PlayerGui.Notify.Text.Visible = true
    game.Players[cn].PlayerGui.Notify.Text.BackgroundTransparency = .8
    game.Players[cn].PlayerGui.Notify.Text.TextTransparency = 0
    game.Players[cn].PlayerGui.Notify.Text.Text = "YOU are un-authorized."
    game.Players[cn].PlayerGui.Notify.Sound:Play()  
    end

script.Parent.ClickDetector.MouseClick:connect(lever)

But for me It says I am authorized(As expected) For my friend Cap it says Un-Authorized. (Not expected) And for my un-authorized friends it says they are authorized (Not expected) so it is basically vise-versa for everyone in the script except me.

1 answer

Log in to vote
0
Answered by
Scriptree 125
9 years ago

Ok, firstly, you were missing two ends, secondly, you don't need that else, if you have nothing under it. Also, for elseifs, they have to be under the if statement, before the if statement ends, otherwise it will all go wrong. An example of what I mean:

if game.Workspace.Part then
    -- if you write an elseif here, it would be for "game.Workspace.Part"
    if game.Workspace.Part.Value then 
        -- if you write the elseif between here, this will be for the "if game.Workspace.Part.Value then"
    end
    -- or if you write an elseif here, it will be for the "game.Workspace.Part"
end

So I hope that explains it.

The fixed script for you would be the following:

function lever(click)
    local cn = click.Name
    local b = game.ServerStorage.Script:Clone()
    local a = Instance.new("BoolValue", game.Players[cn].PlayerGui)
    a.Name = "Value"
    a.Value = false
    if click.Name == "doctorbenny2011" or click.Name ==  "CaptainTheTimeLord" or click.Name == "Player1" or click.Name == "Player" then
        if game.Players[cn].PlayerGui.Value.Value == false then -- A value so the gui only shows once.
            b.Parent = game.Players[cn].PlayerGui.Notify.Text   
            b.Disabled = false  
            a.Value = true  
            game.Players[cn].PlayerGui.Notify.Text.Visible = true
            game.Players[cn].PlayerGui.Notify.Text.BackgroundTransparency = .8
            game.Players[cn].PlayerGui.Notify.Text.TextTransprency = 0
            game.Players[cn].PlayerGui.Notify.Text.Text = "YOU are authorized."
            game.Players[cn].PlayerGui.Notify.Sound:Play()
        end
    else 
        print(click.Name)
        b.Parent = game.Players[cn].PlayerGui.Notify.Text
        b.Disabled = false
        game.Players[cn].PlayerGui.Notify.Text.Visible = true
        game.Players[cn].PlayerGui.Notify.Text.BackgroundTransparency = .8
        game.Players[cn].PlayerGui.Notify.Text.TextTransparency = 0
        game.Players[cn].PlayerGui.Notify.Text.Text = "YOU are un-authorized."
        game.Players[cn].PlayerGui.Notify.Sound:Play()
    end  
end

script.Parent.ClickDetector.MouseClick:connect(lever)

So here, this should work. Hope it helped!

Note: Please tab correctly next time, it makes it way easier to read.

Ad

Answer this question