I had ChatGPT generate code for a small idea I had. If something didn’t work, I’d first let it try to fix the issue. Most of the time (about 90%), it managed to correct itself. If not, I’d step in and fix it quickly. I handled the hit animations with the logs myself. There are two separate programs: melee unit and ranged unit. The ranged unit required some creative problem-solving because I kept running into an issue with its stopping distance before firing. In the end, I had to fix that part myself.
I’m thinking of doing something with this—maybe a game where you place units, battle others, and earn money, etc. Is the code perfect? No, but compared to writing it all myself, it saved me about five hours of work. I had to be creative with my prompts, especially in explaining what I wanted the program to do. If there was an issue, I’d just describe it, and ChatGPT would work on a fix.
ChatGPT made:
- Movement
- Attacking system
- Update movement if enemy changed positions
- Face the enemy while attacking
Woud love to hear what your thoughts are. Not trying to debate if using it is right or wrong.
Code below is the melee unit ChatGPT had programmed. (After several fixes)
local npc = script.Parent
local humanoid = npc:FindFirstChildOfClass("Humanoid")
local hrp = npc:FindFirstChild("HumanoidRootPart")
local WhichTeam = script.Parent.Parent
local WhoToAttack = game.Workspace.WhoToAttack:FindFirstChild(WhichTeam.Name)
local enemiesFolder = game.Workspace:FindFirstChild(WhoToAttack.Value)
if not humanoid or not hrp or not enemiesFolder then
warn("Missing components in NPC or Enemies folder!")
return
end
-- Walking animation setup
local walkAnim = Instance.new("Animation")
walkAnim.AnimationId = "rbxassetid://10921269718"
local animator = humanoid:FindFirstChildOfClass("Animator") or humanoid:WaitForChild("Animator")
local walkAnimation = animator:LoadAnimation(walkAnim)
-- Attack animation setup
local attackAnim = Instance.new("Animation")
attackAnim.AnimationId = "rbxassetid://140229763176741"
local attackAnimation = animator:LoadAnimation(attackAnim)
-- Function to stop the walking animation when NPC is idle
local function onRunning(speed)
if speed > 0 then
if not walkAnimation.IsPlaying then
walkAnimation:Play()
end
else
if walkAnimation.IsPlaying then
walkAnimation:Stop()
end
end
end
humanoid.Running:Connect(onRunning)
-- Remove NPC body after death
local function onDeath()
wait(1) -- Wait 1 second before removing the body
npc:Destroy()
end
humanoid.Died:Connect(onDeath) -- Connect death event
local function getClosestEnemy()
local closestEnemy = nil
local shortestDistance = math.huge
for _, enemy in pairs(enemiesFolder:GetChildren()) do
local enemyHumanoid = enemy:FindFirstChildOfClass("Humanoid")
local enemyHRP = enemy:FindFirstChild("HumanoidRootPart")
if enemyHumanoid and enemyHumanoid.Health > 0 and enemyHRP then
local distance = (hrp.Position - enemyHRP.Position).Magnitude
if distance < shortestDistance then
shortestDistance = distance
closestEnemy = enemy
end
end
end
return closestEnemy
end
local function moveToEnemy()
while humanoid.Health > 0 do -- Only run while alive
local target = getClosestEnemy()
if not target then
break
end
local targetHumanoid = target:FindFirstChildOfClass("Humanoid")
local targetHRP = target:FindFirstChild("HumanoidRootPart")
if not targetHumanoid or not targetHRP then
task.wait(0.5)
continue
end
while targetHumanoid.Health > 0 do
local distance = (hrp.Position - targetHRP.Position).Magnitude
if distance <= 10 then
-- Stop moving
humanoid:Move(Vector3.new(0, 0, 0))
if walkAnimation.IsPlaying then
walkAnimation:Stop()
end
-- Face the enemy
hrp.CFrame = CFrame.new(hrp.Position, Vector3.new(targetHRP.Position.X, hrp.Position.Y, targetHRP.Position.Z))
-- Play attack animation
if not attackAnimation.IsPlaying then
attackAnimation:Play()
end
-- Deal damage
targetHumanoid:TakeDamage(math.random(10,15))
wait(1) -- Attack delay
if (hrp.Position - targetHRP.Position).Magnitude > 10 then
break
end
else
humanoid:MoveTo(targetHRP.Position)
end
task.wait(0.1) -- Update movement frequently
end
task.wait(0.5)
end
end
moveToEnemy()