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
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.
if
s and elseif
s 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 elseif
s 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)