Does my code have any bad practices that might affect the performance in any way?

VERY UNFINISHED CODE!! though what you see here - i’ll use the same principle for everything else if there’s nothing wrong
EDIT: fuhh i also forgot to remove repeating parts

local _services = {
	Server = game:GetService("ServerStorage")
}	--> [ going to add more services later on. if needed. maybe. ] <--}

local Pattern: { { [string]: any } } = {
	{ 
		["tipazh"] = "Offensive" :: string,
		["attack"] = "Pierce" :: string,
		["animation"] = 1234567890 :: number
	},
	{
		["tipazh"] = "Defensive" :: string,
		["animation"] = 0987654321 :: number
	}
}

return function(plr: Player, name: string): (Player,  string) -> any | { [string]: () -> any }
	local card: Configuration = _services.Server.Cards:FindFirstChild(name)
	if not card then
		return error(("Card '%s' not found."):format(name))
	end
	local confAttributes: { any } = card:GetAttributes()
	return {
		MuhAttributes = function()
			local card: Configuration = _services.Server.Cards:FindFirstChild(name)
			if not card then
				return error(("Card '%s' not found."):format(name))
			end
			for _, attribute: string in string.split(confAttributes["attributes"], "|") do
				local result: ModuleScript = _services.Server.Attributes:FindFirstChild(attribute)
				if not result then
					continue
				end
				print(attribute) --> [ for debugging purposes ] <--
				require(result)(plr)
			end
		end
	}
end

this is pretty barebones but i just wanna know beforehand, since i kinda don’t want the performance to die
any criticism is welcome as long as it’s actual criticism and not just hate!
also this code is not going to be seen by anyone else because there’s only 2 people in my “dev team” (me and my friend, he’s a builder)

forgot to say, this is for a single-player game, so most likely not very secure

2 Likes

Yes, it does.

Does my code have any bad practices that might affect the performance in any way?

You have a very deep closure chain with a lot of upvals, and for no reason, you do million indexes at a time and overcomplicate code for not just no gain but the opposite of gain.

Also, you need to rethink the safety of your code. I can’t say much more since I know no context.

Here is a simplified and optimized version of your code. Also, your code is eligible for native compilation:

--!strict
--!optimize 2
--!native
local ServerStorage = game:GetService("ServerStorage")
local Cards = ServerStorage.Cards
local Attributes = ServerStorage.Attributes


return function(plr: Player, name: string): (Player,  string) -> any
	local card: Configuration = Cards:FindFirstChild(name)
	if not card then
		error("Card "..name.." not found.")
	end
	local confAttributes = card:GetAttributes()
	local atrArray = string.split(confAttributes["attributes"], "|")
	return function():()
		for _, attribute: string in atrArray do
			local result: ModuleScript = Attributes:FindFirstChild(attribute)
			if not result then continue end
			print(attribute) --> [ for debugging purposes ] <--
			;(require(result)::(Player)->())(plr)
		end
	end
end

Remember, performance in Luau comes from reducing lookups and closure depth, not from adding layers of structure.

2 Likes

hello, just a question, where can I find the documentation for these functions strict, optimize, native, etc?

Next time please send me a dm instead of bumping thread;
Its not a functions, its a flags

2 Likes

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