Help me make this Script shorter

I tried to do some thinking but couldn’t come up with anything, hopefully one of you can help.

local replicatedStorage = game:GetService("ReplicatedStorage")
local modules = require(replicatedStorage.Modules)

-- stage name is being set from a different function, so don't mind it
function check(player, stage)
	waiting = true
	Answer = false
	
	if stage == "Stage1" then
            -- 5 different letters are sent
		modules.Item(player, "a", "b", "c", "d", "e")

            -- player returns 1 letter
		replicatedStorage.Options.OnServerInvoke = function(player, letter)
         -- if he returned the correct letter it will return true (remote function is used)
			if option == "a" then
				Answer = true
				waiting = false
				return true
			else
				waiting = false
				return false
			end
		end
	elseif stage == "Stage2" then
		modules.Item(player, "f", "g", "h", "i", "j")
		replicatedStorage.Options.OnServerInvoke = function(player, letter)
			if option == "g" then
				Answer = true
				waiting = false
				return true
			else
				waiting = false
				return false
			end
		end
	end
	repeat wait(1) print("Waiting...") until not waiting
end

I am going to have 100 stages and I don’t know if it will be efficient. The thing is, each stage will have 5 different letters each time, and the correct answer will always be different.

I don’t think you need to do return false if it isn’t a local function that checks something. You could also make this way more efficent if you put every stage in a folder and use a for loop to loop every stage instead of writing the same code for every stage.

You could make a table containing possible letters for every stage if every stage has different letters.

local Keys = {
Stage1 = {"a", "b", "c", "d", "e"}
Stage2 = {"f", "g", "h", "i", "j"}
}

Here is my crack at a solution for you, let me know what you think.

local replicatedStorage = game:GetService("ReplicatedStorage")
local modules = require(replicatedStorage.Modules)

local stages = {
	Stage1 = {answer = "a", options = {"a", "b", "c", "d", "e"}},
	Stage2 = {answer = "g", options = {"f", "g", "h", "i", "j"}}
}


-- stage name is being set from a different function, so don't mind it
function check(player, stage)
	waiting = true
	Answer = false
	
	if not stages[stage] then return false end
	
	modules.Item(player, unpack(stages[stage].options)) 
	
	replicatedStorage.Options.OnServerInvoke = function(player, letter)
		local guess = (option == stages[stage].answer)
		Answer = guess
		waiting = false
		return guess
	end
	
	repeat wait(1) print("Waiting...") until not waiting
end
3 Likes

Wow dude this is crazy, thank you so much it works wonderfully, I hope I’ll be this good with scripting one day haha, it would be amazing if you could explain a little how it works

Hey lolzxc123!

I can explain it a bit for you. The big idea behind why this works is I turned each separate stage into a data model that has one answer and all the options in it, as you can see at the top. This method makes it so we can easily add stages to our, what I assume to be a quiz system.

Now the meat and potatoes, the check function. First, we set up our variables waiting which you used to check if we’ve received an answer, and Answer which is a boolean of whether they got the question right or not.

We check if the stage we’re checking exists in our stages dictionary which holds all of our question data models and it it doesn’t we can assume the question is wrong so we return false. Usually, a good idea to add a warning or something here too.

Next, we use modules.Item. I don’t really know what this is doing behind the scenes but I noticed for each of your stages you included the player and all the answers for that stage. So I put the player as the first argument then I used unpack to distribute the options for the stage as separate arguments for the rest of the function call.

Finally, the OnServerInvoke for the Options remote function. First, we know that if our
“option” (I assume you changed this to the letter variable. I just copied what you had.) variable is the same as the answer to the stage then the guess is correct! So the first line we just get the result if it’s the same or not (true or false) and set it to guess so we can use it for our other variables. We set answer to true/false whatever guess turns out to be, it’s false by default so if we set it to false again it won’t really do anything. We set waiting to false because we do that no matter what answer they get. Then finally we return back to the player if they got it wrong or now which is the guess.

Some more: I don’t really see what Answer is doing here you might want to just remove it entirely, I left it in assuming you might want to do something with it. As I said before the option variable isn’t really defined so I assume that is just the letter variable.

Let me know if you have any more questions.

1 Like

Thank you so much for explaining everything so well, I now get it.

And you were basically right about everything haha, I did forget to change the option, and yes there is a use for the ‘Answer’ later in a different function :stuck_out_tongue:

Again I am blown away how you managed to understand what I was trying to achieve while I made it look so messy haha, thanks again for you time!

1 Like