Code Feedback for a game im making

How could I improve my code or what could I change / edit

local core = {}

function core.initalize()
	local BadgesModule = require(script.Icons)
	local Players = game:GetService("Players")

	Players.PlayerAdded:Connect(function(obj)
		obj.CharacterAdded:Connect(function(char)
			if char:FindFirstChild("Head") then
				local logs = setmetatable({},{
					__call = function(self, log)
						table.insert(self, log)
					end,
				})
				logs("core: got head")
				local head = char:FindFirstChild("Head")

				local newProfile = script.Profile:Clone()
				logs("core: new profile created")
				local Info = newProfile.Info
				local Badges = newProfile.Badges

				Info.NameLabel.Text = obj.Name
				Info.NameShadow.Text = obj.Name

				Info.RankLabel.Text = "Creator" -- temp until group is created
				Info.RankShadow.Text = "Creator" -- temp until group is created

				Info.TeamLabel.Text = (obj.Team and obj.Team.Name) or "Missing"
				Info.TeamShadow.Text = (obj.Team and obj.Team.Name) or "Missing"

				Info.TeamLabel.TextColor3 = (obj.Team and obj.TeamColor.Color) or Color3.fromRGB(255, 255, 255)

				newProfile.Parent = head

				logs("core: labels set, doing badges")

				local Adonis = _G.Adonis

				if not Adonis then
					logs("badges: no adonis, running loop")
					repeat Adonis = _G.Adonis wait(.2) until _G.Adonis and Adonis == _G.Adonis
					logs("badges: got adonis, ending loop")
				end

				if Adonis.Access("Adonis_#$*(&!@(218341234543@$@!@#!#@#!353458)#($_#", "Admin").GetLevel(obj) >= 1000 then
					BadgesModule.newIcon(BadgesModule.Creator, obj)
					logs("badges: Added creator, level: "..tostring(Adonis.Access("Adonis_#$*(&!@(218341234543@$@!@#!#@#!353458)#($_#", "Admin").GetLevel(obj)))
				end

				if Adonis.Access("Adonis_#$*(&!@(218341234543@$@!@#!#@#!353458)#($_#", "Admin").CheckAdmin(obj) == true then
					BadgesModule.newIcon(BadgesModule.Moderator, obj)
					logs("badges: Added moderator, true")
				end

				if Adonis.Access("Adonis_#$*(&!@(218341234543@$@!@#!#@#!353458)#($_#", "Admin").IsPlayerDeveloper(obj) == true then
					BadgesModule.newIcon(BadgesModule.Developer, obj)
					logs("badges: Added Developer, true")
				end

				logs("core: finished badges or no badges to set")

				local string = "\n--------------------------------------------------------------------------------------\n".."Profile Logs for "..obj.Name..": \n--"..table.concat(logs, "\n-- ", 1).."\n--------------------------------------------------------------------------------------"

				warn(string)
			else
				warn("no head")
			end
		end)
	end)
end

function core:player_fetchProfile(obj : Player)
	local char = obj.Character or nil
	
	if char then
		local head = char:FindFirstChild("Head") or nil
		
		if head then
			local profile = head:FindFirstChild("Profile")
			
			if profile then
				return profile
			else
				return nil
			end
		else
			return nil
		end
	else
		return nil
	end
end

function core:player_setRankText(obj, text)
	local profile = core:player_fetchProfile(obj)
	
	if profile then
		profile.Info.RankLabel.Text = text
		profile.Info.RankShadow.Text = text
	end
end

function core:player_resetRank(obj)
	core:player_setRankText(obj, "Creator") -- temp until group
	warn("PROFILE :: player_resetRank still using \"Creator text, reset when group")
end

return core

I do have an API key for adonis, I just removed it from the discussion text

Images

2 Likes

Is it really that hard to just give human feedback? This is so obviously a AI response.

7 Likes

Your refactored version looks solid! The optimizations in logging, redundant checks, and function modularization all contribute to a cleaner, more maintainable script. A few additional considerations:

Further Modularization of Badge Assignments

    You could create a assignBadges(player, BadgesModule, Access, Badges) function to handle all badge-related logic separately.

Event Disconnections for Memory Management

    Consider storing event connections in a table and disconnecting them when a player leaves to prevent memory leaks.

Performance Enhancements

    If the badge-checking system is heavy, caching the Access.GetLevel(player) call might help avoid redundant function executions.

Logging Improvements

    If this script runs on many players at once, you might want to limit log sizes or add a debug mode toggle.

Would you like me to implement any of these, or do you have any specific feature requests? :rocket:
0/2
You said:
Implement all and give an overview
ChatGPT said:

Here is the enhanced version of your core module, incorporating the following new improvements:
:white_check_mark: New Features & Optimizations:

Badge Assignment Modularization

    Moved badge assignment logic to a dedicated assignBadges() function to keep the initialize() function clean.

Event Disconnection for Memory Management

    Stored player-related event connections and cleaned them up when the player leaves, preventing potential memory leaks.

Performance Enhancements

    Cached the result of Access.GetLevel(player) to avoid redundant calls during badge assignment.

Enhanced Logging

    Added a DEBUG_MODE flag to control verbose logging. This prevents unnecessary log spamming in production.

:scroll: Refactored Code:

local core = {}

local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)

local DEBUG_MODE = true – Toggle for detailed logs

function core.initialize()
local BadgesModule = require(script.Icons)

local playerConnections = {}

Players.PlayerAdded:Connect(function(player)
	local function cleanup()
		if playerConnections[player] then
			for _, conn in ipairs(playerConnections[player]) do
				if conn.Connected then
					conn:Disconnect()
				end
			end
			playerConnections[player] = nil
		end
	end

	local charConn = player.CharacterAdded:Connect(function(character)
		local head = character:FindFirstChild("Head")
		if not head then
			warn("No head found for " .. player.Name)
			return
		end

		local logs = {}
		local function log(message)
			if DEBUG_MODE then
				table.insert(logs, message)
			end
		end

		log("core: Character added for " .. player.Name)

		-- Clone Profile UI
		local newProfile = script.Profile:Clone()
		log("core: New profile cloned")
		local Info = newProfile.Info
		local Badges = newProfile.Badges

		-- Utility: Set Label Text
		local function setLabel(label, shadow, text)
			label.Text = text
			shadow.Text = text
		end

		-- Set Basic Labels
		setLabel(Info.NameLabel, Info.NameShadow, player.Name)
		setLabel(Info.RankLabel, Info.RankShadow, "Creator") -- TEMPORARY
		setLabel(Info.TeamLabel, Info.TeamShadow, player.Team and player.Team.Name or "Missing")

		Info.TeamLabel.TextColor3 = player.Team and player.TeamColor.Color or Color3.fromRGB(255, 255, 255)

		-- Attach Profile to Head
		newProfile.Parent = head
		log("core: Profile attached to head")

		-- Handle Badge Assignment
		local function waitForAdonis()
			local Adonis = _G.Adonis
			while not Adonis do
				log("Waiting for Adonis to load...")
				wait(0.2)
				Adonis = _G.Adonis
			end
			log("Adonis loaded successfully")
			return Adonis
		end

		local Adonis = waitForAdonis()
		local Access = Adonis.Access("Adonis_#$*(&!@(218341234543@$@!@#!#@#!353458)#($_#", "Admin")

		core:assignBadges(player, BadgesModule, Access, Badges, log)

		-- Output Logs (Only in Debug Mode)
		if DEBUG_MODE then
			warn("\n----- Profile Logs for " .. player.Name .. " -----\n" .. table.concat(logs, "\n") .. "\n-----------------------------------------------")
		end
	end)

	local leaveConn = player.AncestryChanged:Connect(function(_, parent)
		if not parent then
			cleanup()
			log("Cleaned up connections for " .. player.Name)
		end
	end)

	playerConnections[player] = { charConn, leaveConn }
end)

end

– Assigns badges to the player
function core:assignBadges(player, BadgesModule, Access, Badges, log)
local level = Access.GetLevel(player) – Cache this value

if level >= 1000 then
	BadgesModule.newIcon(BadgesModule.Creator, player)
	log("Assigned Creator badge (Level: " .. tostring(level) .. ")")
end

if Access.CheckAdmin(player) then
	BadgesModule.newIcon(BadgesModule.Moderator, player)
	log("Assigned Moderator badge")
end

if Access.IsPlayerDeveloper(player) then
	BadgesModule.newIcon(BadgesModule.Developer, player)
	log("Assigned Developer badge")
end

log("Badge assignment complete")

end

– Fetch Profile from Player
function core:player_fetchProfile(player)
local character = player.Character
if not character then return nil end

local head = character:FindFirstChild("Head")
if not head then return nil end

return head:FindFirstChild("Profile")

end

– Set Rank
function core:player_setRankText(player, text)
local profile = self:player_fetchProfile(player)
if profile then
profile.Info.RankLabel.Text = text
profile.Info.RankShadow.Text = text
end
end

– Reset Rank
function core:player_resetRank(player)
self:player_setRankText(player, “Creator”) – TEMPORARY UNTIL GROUP
warn(“PROFILE :: player_resetRank still using ‘Creator’ text, reset when group is set up.”)
end

return core

:bar_chart: Summary of Changes:

Badge Assignment Modularization

    Extracted badge logic to core:assignBadges() for better readability and reuse.

Event Disconnection for Memory Management

    Tracked and disconnected player-specific events using playerConnections.

    Cleaned up events when the player leaves to prevent memory leaks.

Performance Enhancements

    Cached Access.GetLevel(player) in assignBadges() to avoid redundant calls.

    Reduced _G.Adonis lookups by storing it locally.

Enhanced Logging

    Introduced a DEBUG_MODE flag to control whether logs are printed.

    Consolidated log output at the end for better clarity and performance.

:pushpin: Benefits of This Refactoring:

Better Organization: Complex logic is separated into dedicated functions.

Performance Gains: Cached values and fewer global lookups.

Improved Memory Usage: Event connections are properly managed and disconnected.

Cleaner Logs: Easier to debug with a toggle to disable logs in production.

Would you like any additional features or further refinements? :rocket:

I fixed it dude you gotta thank me (This the dumbest stuff ive ever done on fourm)

1 Like

hey thats mine thank you for the credit

your welcome I hope to work with you again soon

1 Like

This is not in fact your code. I scripted it after making my profile gui. Please do not claim it as your own code, your game “East Hill Highschool” already has a group.

Im just gonna close it if I can’t get any legitimate feedback.

From the looks of it, you took his comment out of context my friend. he was talking about the revision of your code that he wrote.