Better ways of structing a script

I have a script for the enemy characters in a game me and friends are making that handles every enemy, although, I’m concerned, as the script is starting to look messy, is there any way to structure this better?

Here is a extract of the script with one of the mechanics:

local seenGlimpse
function Mechanics.Glimpse()
	local posAsOfRn = circleModel.PrimaryPart.CFrame
	randomizeInCircle(AttackData[currentCPU]["Glimpse"]["HalfAngleTeleport"]["Min"], AttackData[currentCPU]["Glimpse"]["HalfAngleTeleport"]["Max"])
	seenGlimpse = false
	changeModelVisibility(0)
	local renderStepped = RunService.RenderStepped
	for i, v in pairs(currentPlushModel.Animations.Glimpses:GetChildren()) do
		local animationController = currentPlushModel.AnimationController
		glimpseAnimation = animationController:LoadAnimation(v)
	end
	
	glimpseAnimation:Play(0)
	local findingOutGlimpseResult = task.spawn(checkingIfSeenGlimpse)
	
	connection = renderStepped:Connect(function()
		if findingOutGlimpseResult == true then
			print("glimpse stopped")
			canGlimpse = false
			circleModel.PrimaryPart.CFrame = posAsOfRn
			task.spawn(glimpseCoolDown)
			connection:Disconnect()
			task.wait(1)
			glimpseAnimation:Stop(0)
		end
		local isOnScreen = select(2, Camera:WorldToViewportPoint(currentPlushModel.RootPart.Position))
		if isOnScreen == true then
			task.wait()
			canGlimpse = false
			circleModel.PrimaryPart.CFrame = posAsOfRn
			task.spawn(glimpseCoolDown)
			task.cancel(findingOutGlimpseResult)
			game.ReplicatedStorage.Events.Bindable.CloakModel:Fire(currentPlushModel)
			connection:Disconnect()
			task.wait(1)
			glimpseAnimation:Stop(0)
		end
	end)
end

It looks fine, just add comments/documentation to better show what’s going on

That’s probably what’s making it look messy to me, thanks, I’ll start adding some now

1 Like