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

Making secret codes in game?

Asked by 9 years ago

I am making a game with secret codes. When you press the button "submit", it checks if they had put in a correct code. I haven't tested this yet, but it is 95 percent it won't work. This is in a local script in starterGui:

code1 = 1643543
code2 = 3728492
code3 = 5638593
code4 = 5483759
code5 = 4759485
code6 = 4739583
weapon1 = game.ReplicatedStorage.Weapon1
weapon2 = game.ReplicatedStorage.Weapon2
weapon3 = game.ReplicatedStorage.Weapon3
weapon4 = game.ReplicatedStorage.Weapon4
weapon5 = game.ReplicatedStorage.Weapon5
weapon6 = game.ReplicatedStorage.Weapon6
textButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton
textBox = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextBox
backPack = game.Players.LocalPlayer.Backpack

textButton.MouseButton1Down:connect(function()
if textBox.Text = code1 then
local weapon1b = weapon1:clone()
weapon1b.Parent = backPack
elseif textBox.Text = code2 then
local weapon2b = weapon2:clone()
weapon2b.Parent = backPack
elseif textBox.Text = code3 then
local weapon3b = weapon3:clone()
weapon3b.Parent = backpack
elseif textBox.Text = code4 then
local weapon4b = weapon4:clone()
weapon4b.Parent = backPack
elseif textBox.Text = code5 then
local weapon5b = weapon5:clone()
weapon5b.Parent = backpack
elseif textBox.Text = code6 then
local weapon6b = weapon6:clone()
weapon6b.Parent = backpack
end

1 answer

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

You don't have an end for the function or a ) for the ( on line 17. Tab your code correctly.


= is a completely different thing from ==.

= means "set" or "assign" -- it's a command.

== means "compare?", or "are these the same?" -- it's a question.


ifs and elseifs need to use ==, not =.


== means "are these the same" -- which includes that they are the same type of thing. Text is a string (the type of text) while code1, etc are numbers.

They have to be the same type -- "100" == 100 is false.

The simplest solution would be to change the definition of code1 etc to use strings:

code1 = "1643543"

Your code is really repetitive.

The big tree of elseifs could be changed into a neat little dictionary.

A dictionary turns keys (in this case, it would be the codes) into values (in this case, it would be the weapons).

Defining a dictionary looks like this:

codes = {
    ["1643543"] = game.ReplicatedStorage.Weapon1,
    ["3728492"] = game.ReplicatedStorage.Weapon2,
    ["5638593"] = game.ReplicatedStorage.Weapon3,
    ["5483759"] = game.ReplicatedStorage.Weapon4,
    ["4759485"] = game.ReplicatedStorage.Weapon5,
    ["4739583"] = game.ReplicatedStorage.Weapon6,
}

Now you can just check if there is actually a weapon for a given code:

codes = {
    ["1643543"] = game.ReplicatedStorage.Weapon1,
    ["3728492"] = game.ReplicatedStorage.Weapon2,
    ["5638593"] = game.ReplicatedStorage.Weapon3,
    ["5483759"] = game.ReplicatedStorage.Weapon4,
    ["4759485"] = game.ReplicatedStorage.Weapon5,
    ["4739583"] = game.ReplicatedStorage.Weapon6,
}

textButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton
textBox = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextBox
backPack = game.Players.LocalPlayer.Backpack

textButton.MouseButton1Down:connect(function()
    local weapon = codes[ textBox.Text ]
    if weapon then
        -- if the weapon exists (because they entered a real code)
        weapon:Clone().Parent = backPack
    end
end)
0
Should this be in a local script in StarterGui? NeonicPlasma 181 — 9y
0
You use LocalPlayer, so yes, this has to be a LocalScript. LocalScripts usually go in the StarterGui. BlueTaslem 18071 — 9y
Ad

Answer this question