How do I avoid continuously using if statements

Ok so I know I’m gonna sound like an idiot for asking this, but like I seriously don’t know any other way to do this. I can actually think of a few other ways, but for some reason my brain just cannot finish the thought. It’s like it just gets tangled and completely forgets reason. I’m trying to avoid using so many if statements because I know that this 100% is not the optimal way to do this.


As you can see, it looks pretty bad.
I’m thinking of using a module script or a table or a for loop in some way but I don’t really know how I could string those together to make something

1 Like

learn to extract your code bro, use functions or something :skull:

2 Likes

Yeah I probably should make some giveTool() function cause I keep having to give the player stuff but I never did idk why

probably another one for altering the body values in the humanoid

just practice extracting your code efficiently

i dont know what you mean by that

watch never nesting tutorials on youtube

I advice to use ModuleScript for these kind of things, you would have to play around but maybe a function in each line.

One method you could use is replacing the Events table with a dictionary that stores functions for each different type of event, then indexing the table with the event:

local Events = {
	Event1 = function()
		-- Do something
		print("Hello!")
	end,
	Event2 = function()
		-- Do something else
		print("Goodbye!")
	end,
}

local event = "Event1"

Events[event]() -- Hello!

event = "Event2"

Events[event]() -- Goodbye!

I wish something that works like C++'s switch was possible in Luau though, it would provide a way to solve this problem without having to resort to methods like the one above

3 Likes

ok first of all, use if elseif, not if if if, because in elseif if your first condition is true the other condition gets skipped, otherwise in if if if all the conditions are checked, I assume from your code there can only be one result so you should use elseif for these situations, secondly, you can create a table for these if you have alot of if statements for ex

local _eventFunctions = {
 Event[1] = function()  local size = 5; humnaoid.size = size; .....  end;
 Event[2] = function() Carl.Parent = workspace; ....  end;
 Event[3] = function()  lasergun.Parent = player.Backpack  end;
--so on
}

--Your code then will just become:
_eventFunctions[event]()

script how you want. it only matters that you can read it and it’s optimized.

As JohhnyLegoKing suggested, keep your events inside a table of events, just make sure that the event is a thing before calling it. So doing the following will prevent any errors

if Events[event] then
	Events[event](pass_variables_onto_event)
end
1 Like

You should also be sending parameters, so you don’t have multiple functions doing the same thing to different objects.

Wouldn’t that just be doing the same thing, except it looks smaller?
Because each function would have to have different parameters, which means I would still have to check each thing.

sort of sort of not. If i get what youre saying, you may be looking for something called a variadic function

local functionDir = {
f1 = function(player: Player, string: String)
     --can recieve one set of arumgents here
end
f2 = function(someNumber)
     --can recieve a completley different number and set of args here
end
}

local function dispatchFunction(f,...) --... is a tuple of all arguments passed after String f
     functionDir[f](...)
end
event:Connect(dispatchFunction)

Looking at your code, we can apply a similar concept and abstract a lot of it into generalized functions

local functionDir = {
giveItem = function(player,item)
     item.Parent = player.Backpack
end
parentToWorkspace = function(instance,newPos)
     instance.Parent = workspace
     instance:MoveTo(newPos)
end
}

local function dispatchFunction(f,...) --... is a tuple of all arguments passed after String f
     functionDir[f](...)
end
event:Connect(dispatchFunction)

The way it works is like so: A dictionary is created with the index being the different events possible, and the values being the functions that run according to which event is indexed

Let’s say you want that if event is equal to event 2, you’d like to change the color of the baseplate. To do that, this is what you’ll need to do (I’m also using the method suggested by @TortenSkjold since it’s quite good):

local Events = {
	Event1 = function()
		-- Do something
		print("Hello!")
	end,
	Event2 = function()
		workspace.Baseplate.Color = Color3.new(0, 1, 0)
	end,
}

local event = "Event2"

if Events[event] then
	Events[event]()
end

It’s not intended to be used like so:

local Events = {
	Event1 = function()
		-- Do something
		print("Hello!")
	end,
	Event2 = function()
		-- Do something else
		print("Goodbye!")
	end,
}

local event = "Event1"

if event == "Event1" then
	Events.Event1()
elseif event == "Event2" then
	Events.Event2()
end

otherwise that would defeat its purpose


@robloxguy9093ALT btw if you’re comfortable with sharing the rest of your code, I can edit it to make it work using this system if you prefer

Ah that makes sense, my bad it was like 1am for me.

1 Like

if this code is all running from the fire of one event, then you shouldnt be doing that.

from my experience having one event that uses some key to figure out what kind of function to run is terribly inefficient and makes the server slow down after a while

you should really be using one event for one function, or one even for functions that are similar

for example if you had a gun:

  • event for firing
  • event for reloading
  • event for setting walkspeed
    etc
1 Like

Wait nevermind, I’m still actually kind of confused. The only way to access these variables from inside the table of functions would be to use parameters, right?
Screen Shot 2024-04-17 at 7.06.25 AM
Unless there’s another way? I don’t really know.

If you’re indexing a dictionary with a constant value, you can use the . operator to do, but if the index is a variable (like in your case), you’ll need to use the square brackets [] to index the dictionary. I’m mentioning this because parameters are the values passed to a function when it’s called, but they’re placed between parentheses () not square brackets []. There doesn’t seem to be a need to pass any parameters to the function with the way your code is currently written, although if in the future you’ll need to do so then you’d need to use if statements once again

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.