I have this pathfinding + damage script, it’s kind of long (230 lines), and it’s EXTREMELY unreadable, even for a novice like me
I figured I’d ask on here since My knowledge about clean code is next to zero.
I’ll take literally any advice on how to make this not an eyesore
local LOSsize = Vector3.new(75,25,75)
local Dummy = script.Parent
local PF = game:GetService("PathfindingService")
local DummyHumanoid = Dummy.HumanoidRootPart
local RS = game:GetService("RunService")
local players = game:GetService("Players")
local db = false
local seed = 0
local isJumping = false
local Humanoid = Dummy:WaitForChild("Humanoid")
local forwardOffset = 2
local Size = Vector3.new(3,7,4)
local db = false
function AttackArea()
local Area = Instance.new("Part")
Area.Name = "AttackBox"
Area.Parent = DummyHumanoid
Area.CFrame = DummyHumanoid.CFrame * CFrame.new(0,0, -forwardOffset)
Area.Size = Size
Area.Transparency = 0.5
Area.CanCollide = false
Area.Anchored = true
game:GetService("Debris"):AddItem(Area, 5)
end
function createAreapreview()
local part = Instance.new("Part")
part.Name = "LoSChecker"
part.Parent = Dummy.HumanoidRootPart
part.CFrame = Dummy.HumanoidRootPart.CFrame
part.Size = LOSsize
part.Color = Color3.fromRGB(255, 0, 4)
part.Anchored = true
part.Transparency = 0.6
part.CanCollide = false
end
createAreapreview()
local DMGsize = Vector3.new(10,7,10)
function createDamageArea()
local part = Instance.new("Part")
part.Name = "DamageDealer"
part.Parent = Dummy.HumanoidRootPart
part.CFrame = Dummy.HumanoidRootPart.CFrame
part.Size = DMGsize
part.Color = Color3.fromRGB(230, 255, 120)
part.Anchored = true
part.Transparency = 0.6
part.CanCollide = false
end
createDamageArea()
local parameterTable = {
AgentRadius = 3,
AgentHeight = 2,
AgentCanJump = true,
AgentCanClimb = false,
WaypointSpacing = 4
}
local dmgDealer = DummyHumanoid.DamageDealer
local losChecker = DummyHumanoid.LoSChecker
local path = PF:CreatePath(parameterTable)
local isJumping = false
local seed = 0
function TraversePath(closestPlayer)
seed += 1
local currentSeed = seed
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success and #waypoints > 0 then
for i = 2, #waypoints, 1 do
local waypoint = waypoints[i]
if waypoint.Action == Enum.PathWaypointAction.Jump then
isJumping = true
Dummy.Humanoid.Jump = true
end
Dummy.Humanoid:MoveTo(waypoints[i].Position)
Dummy.Humanoid.MoveToFinished:Wait()
if waypoint.Action ~= Enum.PathWaypointAction.Jump then
isJumping = false
end
if not isJumping and seed ~= currentSeed then break end
end
end
end
local Weapon = Dummy:FindFirstChildWhichIsA("Tool")
local ComboAnim = Weapon:FindFirstChild("ComboAnim")
local HeavySwing = Weapon:FindFirstChild("HeavySwing")
local animator = Humanoid:WaitForChild("Animator")
local combo = animator:LoadAnimation(ComboAnim)
local heavyattack = animator:LoadAnimation(HeavySwing)
local attackSeed = 0
local connection = nil
local Stab = Weapon:WaitForChild("Stab")
local event = game.ReplicatedStorage:WaitForChild("MeleeEvents"):WaitForChild("DaggerEvents").SoundEvent
local AttackChances = {
[heavyattack] = 60,
[combo] = 40
}
function Attack()
local totalChance = 0
for _,chance in pairs(AttackChances) do
totalChance += chance
end
local rng = math.random(1, totalChance)
for option, chance in pairs(AttackChances) do
rng -= chance
if rng <= 0 then
if option == heavyattack then
heavyattack:Play()
elseif option == combo then
combo:Play()
end
end
end
end
local stabId = "rbxassetid://16815968728"
heavyattack.KeyframeReached:Connect(function(KeyframeName)
local FrontCFrame = DummyHumanoid.CFrame * CFrame.new(0,0, -forwardOffset)
if KeyframeName == "AttackFrame" then
AttackArea()
local Punish = workspace:GetPartBoundsInBox(FrontCFrame, Size)
for _,v in Punish do
if v.Parent:FindFirstChild("Humanoid") and players:GetPlayerFromCharacter(v.Parent) then
if db == false then
db = true
v.Parent:FindFirstChild("Humanoid"):TakeDamage(40)
print("Heavy")
event:FireAllClients(stabId, Weapon)
heavyattack.Ended:Wait()
db = false
break
end
end
end
end
end)
combo.KeyframeReached:Connect(function(KeyframeName)
local FrontCFrame = DummyHumanoid.CFrame * CFrame.new(0,0, -forwardOffset)
if string.find(KeyframeName, "Combo") then
AttackArea()
local Punish = workspace:GetPartBoundsInBox(FrontCFrame, Size)
for _,v in Punish do
if v.Parent:FindFirstChild("Humanoid") and players:GetPlayerFromCharacter(v.Parent) then
v.Parent:FindFirstChild("Humanoid"):TakeDamage(13)
print("Light")
combo.Ended:Wait()
db = false
break
end
end
end
end)
local attacking = false
RS.Heartbeat:Connect(function()
losChecker.CFrame = Dummy.HumanoidRootPart.CFrame
dmgDealer.CFrame = Dummy.HumanoidRootPart.CFrame
local LOS = workspace:GetPartBoundsInBox(losChecker.CFrame, losChecker.Size)
local closestPlayer = nil
for _, v in pairs(LOS) do
if v.Parent:FindFirstChild("Humanoid") then
local targetPlayer = game.Players:GetPlayerFromCharacter(v.Parent)
if targetPlayer then
if closestPlayer == nil or (Dummy.HumanoidRootPart.Position - targetPlayer.Character.HumanoidRootPart.Position).Magnitude < (Dummy.HumanoidRootPart.Position - closestPlayer.Character.HumanoidRootPart.Position).Magnitude then
closestPlayer = targetPlayer
end
end
end
end
if closestPlayer then
losChecker.Color = Color3.fromRGB(123, 255, 93)
if not isJumping then
task.spawn(TraversePath, closestPlayer)
end
local success, errorMessage = pcall(function()
path:ComputeAsync(Dummy.HumanoidRootPart.Position, closestPlayer.Character.HumanoidRootPart.Position)
end)
else
losChecker.Color = Color3.fromRGB(255, 0, 4)
Dummy.Humanoid:Move(Vector3.new(0, 0, 0))
end
----------------DAMAGE DEALER---------------
local DMG = workspace:GetPartBoundsInBox(dmgDealer.CFrame, dmgDealer.Size)
for _,v in DMG do
if v.Parent:FindFirstChild("Humanoid") then
local plr = players:GetPlayerFromCharacter(v.Parent)
if plr then
dmgDealer.Color = Color3.fromRGB(108, 135, 255)
if attacking == true then return end
if attacking == false then
Attack()
attacking = true
coroutine.wrap(function()
task.wait(4)
attacking = false
end)()
end
else
dmgDealer.Color = Color3.fromRGB(230, 255, 120)
end
end
end
end)