My code is hideous, what can I do

My code is pretty hideous. Im not sure what went wrong but what are some things that I can work on. For reference, here is a simple enemy follow script I made
`
local enemiesfolder = game.Workspace.Enemies

while true do
local enemies = enemiesfolder:GetChildren()
for i, v in ipairs(enemies) do
local hum = v.Humanoid
local hrp = v.HumanoidRootPart
local speed = v.Speed
local enemystunned = v.Stunned

	--Stunned
	if enemystunned.Value == true then
		hum.WalkSpeed = 0
	else
		hum.WalkSpeed = speed.Value
		
		--Enemy follow
		local chars = game.Workspace:GetChildren()
		local closestchar
		local closestchardist = 30
		
		for i, v in ipairs(chars) do
			local phum = v:FindFirstChild("HumanoidRootPart")
			if phum then
				--finds closest player
				local phrp = v.HumanoidRootPart
				local distance = (hrp.Position - phrp.Position).Magnitude
				
				if distance < closestchardist then
					closestchardist = distance
					closestchar = v
				end
			end
		end
		
		if closestchar then
			local closesthrp = closestchar.HumanoidRootPart
			hum:MoveTo(closesthrp.Position)
		end
	end
end
task.wait()

end
`

Not sure if its just me but part of the code isnt formatted

local Players = game:GetService('Players')
local Enemys = workspace.Enemies

local function getAllCharacters() : {Model?} -- Returns a table containing all players characters.
	local Characters = {}
	for _, v in pairs(Players) do
		if v.Character then
			table.insert(Characters, v.Character)
		end
	end
	return Characters
end

while true do
	for i, v in pairs(Enemys:GetChildren()) do
		local Humanoid = v:FindFirstChild('Humanoid') :: Humanoid
		local RootPart = v:FindFirstChild('HumanoidRootPart') :: BasePart
		local Stunned = v:FindFirstChild('Stunned') :: BoolValue
		local Speed = v:FindFirstChild('Speed') :: NumberValue
		
		if not Humanoid or not RootPart then continue end -- Skip this enemy because they either died or got destroyed.
		
		-- Stunned
		if Stunned.Value then
			Humanoid.WalkSpeed = 0
		elseif not Stunned.Value then -- Not Stunned
			Humanoid.WalkSpeed = Speed.Value
			
			-- Enemy Follow
			local Characters = getAllCharacters()
			local closest = nil
			local distance = math.huge
			
			for _, Character in pairs(Characters) do
				local Root = v:FindFirstChild('RootPart') :: BasePart
				if Root then
					local Distance = (Root.Position - RootPart.Position).Magnitude
					
					if Distance < distance then
						closest = Character
					end
				end
			end
			
			if closest ~= nil then
				local Root = closest:FindFirstChild('HumanoidRootPart') :: BasePart
				Humanoid:MoveTo(Root.Position)
			end
		end
	end
	task.wait()
end

Can you explain some of the changes you made

Added typing to the variables for intellisense, removed your extra variable ‘phrp’ after already setting a variable to the humanoid root part, and added a function to go through the players and return a table containing all characters so you don’t loop through the entire workspace for them

may i ask if there are difference or benefits to declare type as
local humanoid : Humanoid = character:FindFirstChild(“Humanoid”)
vs
local humanoid = character:FindFirstChild(“Humanoid”) :: Humanoid

Really, type checking in this case might be overused, using it in functions is pretty much the best use-case as others may complicate your code

I’ve wrote a tutorial about Code Organization, here’s the link:

Most of the time your code should be redable and do one thing at a time, using module scripts and good practices is enough for making your code good