Optimization check

return function(Character)
local GetNearestHumanoid = require(script.Parent.Parent.GetNearestHumanoid)
local EnemyTable = require(script.Parent).EnemyTable
local Humanoid = Character:FindFirstChild(“Humanoid”)

if Humanoid and Character.PrimaryPart then
	local Target = GetNearestHumanoid(Character, EnemyTable)
	if Target and Target.Parent and Target.Parent.PrimaryPart then
		local Distance = (Target.Parent.PrimaryPart.Position - Character.PrimaryPart.Position).Magnitude
		Humanoid:MoveTo(Target.Parent.PrimaryPart.Position, Target.Parent.PrimaryPart)

		if Distance < 10 then
			Humanoid.WalkSpeed = EnemyTable.WalkSpeed
		else
			Humanoid.WalkSpeed = EnemyTable.WalkSpeed * 1.5
		end
	end
end

end

can someone tell me how i can optimize this movement code, im making an rpg game

no idea why the code glithced out like that, i cant fix it tho so mb

  1. Require modules outside of the function
  2. If you want to make sure something isn’t parented to nil, or specifically parented to the workspace you should do instance:IsDescendantOf(workspace)

I would also use variables more because there’s a lot of .Parent.PrimaryPart going on.

1 Like

thank you, i heard humanoid:moveto is bad performance wise also but i didnt bother changing it since this isnt a td game and theres not gonna be 500 enemies being ran at once in a radius of 30 studs

forgot to say im not gonna really be checking solutions since im just looking for overall feedback and what i could improve on but i do really appreciate you replying

1 Like