Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Adding multiple codes [?, solved!]

Hello! I'm working on a game that has a code system. But I'm trying to add another code. (I have 1 code in my game now) But I'm struggling to find out how to figure it out! Checking the code if its wrong or right is invoked by a RemoteFunction located in Replicated Storage.

Button that Invokes the RemoteFunction (works fine):

local b = script.Parent
local codeInput = script.Parent.Parent.TextLabel.CodeIput

b.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.CheckCode:InvokeServer(codeInput.Text)
	
	if result == true then
		codeInput.Text = "Success!"
	else
		codeInput.Text = "Code invalid or outdated!"
	end
end)

Checking if the code is right or wrong (error):

game.ReplicatedStorage.CheckCode.OnServerInvoke = function(player, code)
	if code == "Sub2Tenulty" then -- first code
		
		local backpack = player:WaitForChild("Backpack")
		
		local machete = game.ServerStorage["Tenulty's Machete"]:Clone()
		
		machete.Parent = backpack
		
		return true
	else
		return false
	end
	
	if code == "Sub2Kobe" then --second code
		
		local backpack = player:WaitForChild("Backpack")
		
		local machete = game.ServerStorage["Kobe's Machete"]:Clone()
		
		machete.Parent = backpack
		
		return true
	else
		return false
	end
end

2 comments

0

Look at the first line you put OnServerInvoke = function when it should be OnServerInvoke:Connect(function(player, code)

1

onserverinvoke does not use connect because it isn't an event but a callback

2 answers

✓ Accepted answer

The reason your code is not working is because you return true/false within the first if block, meaning your 2nd if block never gets a chance to run. You could just remove the else return false part, or you can merge the if blocks together:

game.ReplicatedStorage.CheckCode.OnServerInvoke = function(player, code)
    if code == "Sub2Tenulty" then -- first code

        local backpack = player:WaitForChild("Backpack")

        local machete = game.ServerStorage["Tenulty's Machete"]:Clone()

        machete.Parent = backpack

        return true
    elseif code == "Sub2Kobe" then --second code

        local backpack = player:WaitForChild("Backpack")

        local machete = game.ServerStorage["Kobe's Machete"]:Clone()

        machete.Parent = backpack

        return true
    else
        return false
    end
end

However, this isn't super efficient. If you had 100 codes and they sent in a wrong code, you'd be going through all 100 codes before you knew that it was wrong. This isn't that big a deal, but we can use tables to keep things efficient:

local codes = {
	Sub2Tenulty = function(player)
		local backpack = player:WaitForChild("Backpack")
	        local machete = game.ServerStorage["Tenulty's Machete"]:Clone()
	        machete.Parent = backpack
	end,
	Sub2Kobe = function(player)
	        local backpack = player:WaitForChild("Backpack")
	        local machete = game.ServerStorage["Kobe's Machete"]:Clone()
        	machete.Parent = backpack
	end,
	["Example of a code with spaces in it"] = function(player)
		-- etc
	end,
}

(Note: You have to use ["This format"] if the code is not a valid variable name. So, if the code starts with anything except a character or _, or if it contains anything besides letters or numbers or _, you need to use it. You can use it for everything if you'd prefer to keep it consistent.)

The idea is that we're storing a mapping between each code and what we want to do when a player enters it. Now to use it:

game.ReplicatedStorage.CheckCode.OnServerInvoke = function(player, code)
	local action = codes[code]
	if not action then -- It wasn't a code
		return false
	end
	-- You could add additional code here to check to see if they've already used it (if you only want to let them use a code once)
	action(player) -- this runs the action associated with the code in the table above, providing the function with the player who sent it in.
	return true
end

2 comments

0

YOu saved my life

0

thank you

You could make a table with the codes, and loop through the table.

-- server
local codes = {"Sub2Tenulty", "Sub2Kobe"} -- and you can add as many as you want

game.ReplicatedStorage.CheckCode.OnServerInvoke = function(player, code)
	for _, v in pairs(codes) do
		if v == code then
			-- i recommend changing the tools' name to something like "Sub2KobeMachete" and "Sub2TenultyMachete" so that it's easier to script
		        local backpack = player:WaitForChild("Backpack")
		
		        local machete = game.ServerStorage[code.."Machete"]:Clone()
			machete.Parent = backpack
			return true
		else
			return false
		end
	end
end 

2 comments

0

When I tested this out, I wrote a 'print(v)' and 'print(#v)'. The results were: Sub2Kobe, 8. It was 8 because i figured out 8 was the amount of letters/numbers in "Sub2Kobe". It didn't print 3. That's a problem.

0

Oops, not 3. but 2. Because there are 2 codes.

This discussion is preserved as it was. New answers, comments, and votes are closed.