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

How do you make 2 codes(testing and power) without it not working?

Asked by 5 years ago
code = "testing" --type your code
code2 = "power"--type your second code
local gear = game.ReplicatedStorage:WaitForChild('Handgun')
Input = script.Parent.Parent.CodeInput
Output = script.Parent
Output.MouseButton1Down:Connect(function()
        if Input.Text == code then
            gear:Clone().Parent = game.Players.LocalPlayer.Backpack
            gear:Clone().Parent = game.Players.LocalPlayer.StarterGear
            if Input.Text == code then Input.Text = 'Correct'
                wait (1)
                Input.Text = 'Enter'
            else
                Input.Text = 'Invalid/Used'
                wait (1)
                Input.Text = 'Enter'
            end
        end
        end)    

code2 = "power" --type your code
code = "testing" --type your second code
local gear = game.ReplicatedStorage:WaitForChild('Sword')
Input = script.Parent.Parent.CodeInput
Output = script.Parent
Output.MouseButton1Down:Connect(function()
        if Input.Text == code2 then
            gear:Clone().Parent = game.Players.LocalPlayer.Backpack
            gear:Clone().Parent = game.Players.LocalPlayer.StarterGear
                        if Input.Text == code2 then 
                Input.Text = 'Correct'
                wait (1)
                Input.Text = 'Enter'
            else
                Input.Text = 'Invalid/Used'
                wait (1)
                Input.Text = 'Enter'
            end
        end
        end)    

i did this thing and it didnt work. i tried like many other solutions like adding if Input.Text == code2 or code then doing it but it didnt work. The Input doesn't change when I put like dfkdj.

1 answer

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

The Input isn't changing for bad values because of your if statements. This is a lot easier to see if you use tabs correctly (this is your 2nd script with proper tabs):

code2 = "power" --type your code
code = "testing" --type your second code
local gear = game.ReplicatedStorage:WaitForChild('Sword')
Input = script.Parent.Parent.CodeInput
Output = script.Parent
Output.MouseButton1Down:Connect(function()
    if Input.Text == code2 then
        gear:Clone().Parent = game.Players.LocalPlayer.Backpack
        gear:Clone().Parent = game.Players.LocalPlayer.StarterGear
        if Input.Text == code2 then 
            Input.Text = 'Correct'
            wait (1)
            Input.Text = 'Enter'
        else
            Input.Text = 'Invalid/Used'
            wait (1)
            Input.Text = 'Enter'
        end
    end
end)

Now it is much more clear that:

  • You are checking whether Input.Text == code2 twice
  • The "else" will never run (it can only run if Input.Text == code2 for the first 'if' and then is changed by the time the 2nd one runs -- which can't happen)

The fix is to delete the 2nd if Input.Text == code then line and an end.

However, your scripts are messy -- you needn't have two slightly different scripts, each with all the codes in them, yet each only handling one of the codes. Instead, you can use a dictionary (or table).

Reference if you haven't used tables before: http://lua-users.org/wiki/TablesTutorial

Code:

local localPlayer = game.Players.LocalPlayer
local function AwardGear(gear) -- clones the gear to the player
    gear:Clone().Parent = localPlayer.Backpack
    gear:Clone().Parent = localPlayer.StarterGear
end
local function GenAwardGear(name) -- generate a function that awards the gear with the specified name
    local gear = game.ReplicatedStorage:WaitForChild(name)
    return function()
        AwardGear(gear)
    end
end
local codes = { -- each key is the code the user is to type; the value is the function to run if they enter it
    ["power"] = GenAwardGear('Sword'),
    ["testing"] = GenAwardGear('Handgun'),
    ["say hello"] = function()
        print("hello")
    end,
    --etc
}
Input = script.Parent.Parent.CodeInput
Output = script.Parent
Output.MouseButton1Down:Connect(function()
    local func = codes[Input.Text]
    if func then
        func()
        -- You could prevent the code from being used again until the player rejoins the server with this next line
        -- codes[Input.Text] = nil
        Input.Text = 'Correct'
        wait(1)
        Input.Text = 'Enter'
    else
        Input.Text = 'Invalid/Used'
        wait(1)
        Input.Text = 'Enter'
    end
end)

Note that this script design is still poor - you should only give the player stuff on the server (unless this is a single player game). The GUI work (like MouseButton1Down) must be on the client, but it should then transmit what the user typed to the server and the server can determine whether it's a valid code or not (and what function to run, if any).

0
i should copy and paste this ?? HappyTimIsHim 652 — 5y
0
the nil value for the code isnt working HappyTimIsHim 652 — 5y
0
Whoops - "Input.Text" is always "Correct" at that point. I moved the line up to before Input.Text is changed; it should work now. (If it wasn't clear, the bottom script replaces your 2 other ones.) chess123mate 5873 — 5y
Ad

Answer this question