Feedback on this TDS like zombie code

Hello, I am wondering if this is a good zombie moving system for a tower defense game I might be creating in the future, and is it possible to recreate this system with half as much code and maybe cleaner? Right now, I am not using humanoids, as they are not necessary.

function Zombies:Move(zombie)
	local uniqueSeed = Random.new()
	task.spawn(function()
		local hrp = zombie:FindFirstChild("HumanoidRootPart")
		local ZOMBIE_OFFSET = uniqueSeed:NextNumber(-0.34, 0.34)
		local Animator = zombie:WaitForChild("AnimationController"):WaitForChild("Animator")
		local anim = Instance.new("Animation")
		if zombie:GetAttribute("WalkAnimation") then
			anim.AnimationId = "rbxassetid://14761002355"
		else
			anim.AnimationId = "rbxassetid://14755070010"
		end
		anim.Parent = Animator
		local animation = Animator:LoadAnimation(anim)
		animation.Looped = true
		animation:Play()
		if zombie:GetAttribute("WalkAnimSpeed") then
			animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / zombie:GetAttribute("WalkAnimSpeed"))
		else
			animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / 4.7)
		end
		for waypoint = 1, #waypoints:GetChildren() do
			local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude
			local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))
			local rx, ry, rz = cframeLookat:ToOrientation()
			local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)
			local orientation = Vector3.new(dx, dy, dz)
			local Time = GetTime(Distance, zombie:GetAttribute("WalkSpeed"))
			local timeToNextWaypoint = (Time * 1.5) -- Increase for slower walking and decrease for faster
			local moveTween = TweenService:Create(hrp, TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2.5), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2.5))}) 
			moveTween:Play()
			local turnSpeedFormula = (Time / Distance) * 1.8 -- Increase for slower turning and decrease for faster
			local orientationTween = TweenService:Create(hrp, TweenInfo.new(turnSpeedFormula, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Orientation = orientation})
			orientationTween:Play()
			task.wait(timeToNextWaypoint - 0.015)
		end
		zombie:Destroy()
	end) 
end

Here is a video of the code in action:

Any ideas are appreciated!

I can’t read this, half the screen is taken up by blank lines. Do you get paid for your lines somehow? :slight_smile:

Lol. I like to space out the code so it makes it easier for me to read. I’ll update it with a normal version.

Yeah, I’m doing this too, just to separate logical blocks of code. But you did this between each line, which don’t make sense. I would make it something like that

function Zombies:Move(zombie)
	local uniqueSeed = Random.new()
	task.spawn(function()
		local hrp = zombie:FindFirstChild("HumanoidRootPart")
		local ZOMBIE_OFFSET = uniqueSeed:NextNumber(-0.34, 0.34)

		local Animator = zombie:WaitForChild("AnimationController"):WaitForChild("Animator")
		local anim = Instance.new("Animation")
		if zombie:GetAttribute("WalkAnimation") then
			anim.AnimationId = "rbxassetid://14761002355"
		else
			anim.AnimationId = "rbxassetid://14755070010"
		end
		anim.Parent = Animator

		local animation = Animator:LoadAnimation(anim)
		animation.Looped = true
		animation:Play()

		if zombie:GetAttribute("WalkAnimSpeed") then
			animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / zombie:GetAttribute("WalkAnimSpeed"))
		else
			animation:AdjustSpeed(1 * zombie:GetAttribute("WalkSpeed") / 4.7)
		end

		for waypoint = 1, #waypoints:GetChildren() do
			local Distance = (hrp.Position - waypoints[waypoint].Position).Magnitude
			local cframeLookat = CFrame.lookAt(hrp.Position, Vector3.new(waypoints[waypoint].Position.X, hrp.Position.Y, waypoints[waypoint].Position.Z))
			local rx, ry, rz = cframeLookat:ToOrientation()
			local dx, dy, dz = math.deg(rx), math.deg(ry), math.deg(rz)
			local orientation = Vector3.new(dx, dy, dz)

			local Time = GetTime(Distance, zombie:GetAttribute("WalkSpeed"))
			local timeToNextWaypoint = (Time * 1.5) -- Increase for slower walking and decrease for faster
			local moveTween = TweenService:Create(hrp, TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2.5), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2.5))}) 
			moveTween:Play()

			local turnSpeedFormula = (Time / Distance) * 1.8 -- Increase for slower turning and decrease for faster
			local orientationTween = TweenService:Create(hrp, TweenInfo.new(turnSpeedFormula, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Orientation = orientation})
			orientationTween:Play()

			task.wait(timeToNextWaypoint - 0.015)
		end
		zombie:Destroy()
	end) 
end

And this line just kills me

local moveTween = TweenService:Create(hrp, TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), {Position = Vector3.new(waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2.5), hrp.Position.Y, waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2.5))})

First of all, it’s a good practice to new line it like this

local moveTween = TweenService:Create(
    hrp, 
    TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), 
    {Position = Vector3.new(
        waypoints[waypoint].Position.X + (ZOMBIE_OFFSET*2.5), 
        hrp.Position.Y, 
        waypoints[waypoint].Position.Z + (ZOMBIE_OFFSET*2.5)
    )}
)

And after it, make it even readable (and even a little more perfomant) by making a variable with waypoints[waypoint].Position

local waypointPos = waypoints[waypoint].Position
local moveTween = TweenService:Create(
    hrp, 
    TweenInfo.new(timeToNextWaypoint, Enum.EasingStyle.Linear), 
    {Position = Vector3.new(
        waypointPos.X + (ZOMBIE_OFFSET*2.5), 
        hrp.Position.Y, 
        waypointPos.Z + (ZOMBIE_OFFSET*2.5)
    )}
)

A lot better, isn’t it? It’s turning out even more handier when you are using splitscreen when coding

1 Like