Prompt
Answer
Lua Sword Animation Handler
This Lua script manages character animations for a sword wielding character in a Roblox game. It includes functions for setting up animations, handling equipping and unequipping, and responding to player actions like running, jumping, and attacking.
Code Overview
The script includes the following key features:
- Animation Setup: Load various animations related to walking, idling, jumping, attacking, and equipping.
- Animation Control: Functions to play and stop animations based on player actions.
- Input Handling: Connects player actions to appropriate animation responses.
Key Improvements
The main task is to ensure that after an attack, if the player is not walking, the idle animation correctly plays. Below is the modified code addressing this requirement.
local Players = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Sword = script.Parent
local Walk
local Idle
local Jump
local Attack
local Equip
local isWalking = false
local isAttacking = false
local function setupAnimations()
workspace:WaitForChild(Player.Name)
Character = Player.Character or Player.CharacterAdded:Wait()
Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
-- Check for Animator
if not Animator then
warn("Animator not found on Humanoid")
return
end
-- Load animations
Walk = Animator:LoadAnimation(rs.Assets.Animations.Walk)
Idle = Animator:LoadAnimation(rs.Assets.Animations.Idle)
Jump = Animator:LoadAnimation(rs.Assets.Animations.Jump)
Attack = Animator:LoadAnimation(rs.Assets.Animations.Attack)
Equip = Animator:LoadAnimation(rs.Assets.Animations.Equip)
if not Equip then
warn("Failed to load Equip animation")
else
print("Equip animation loaded successfully")
end
end
local function stopAnimations()
if Walk then Walk:Stop() end
if Idle then Idle:Stop() end
if Jump then Jump:Stop() end
if Attack then Attack:Stop() end
if Equip then Equip:Stop() end
isWalking = false
isAttacking = false
end
local function onRunning(speed)
if Sword.Parent == Character then
if speed > 0 and not isWalking and Humanoid.MoveDirection.Magnitude > 0 then
isWalking = true
Walk:Play()
elseif speed == 0 and isWalking then
isWalking = false
Walk:Stop()
Idle:Play() -- Start Idle animation when stopping
end
end
end
local function onJumping(isJumping)
if Sword.Parent ~= Character then return end
if isJumping then
Jump:Play()
end
-- Ensure Idle plays if stopping while jumping
if isWalking then
isWalking = false
Walk:Stop()
Idle:Play()
end
end
local function onEquipped()
print("Sword equipped")
if not Equip then
warn("Equip animation is nil, reloading...")
Equip = Humanoid:WaitForChild("Animator"):LoadAnimation(rs.Assets.Animations.Equip)
end
if Equip then
print("Playing Equip animation")
Equip:Play()
else
warn("Equip animation is still nil after reload")
end
Idle:Play()
if Humanoid.MoveDirection.Magnitude > 0 then
isWalking = true
Walk:Play()
end
end
local function onUnequipped()
print("Sword unequipped")
Idle:Stop()
isWalking = false
Walk:Stop()
isAttacking = false
Attack:Stop()
if Equip then
Equip:Stop()
end
end
local function onAttack()
if Sword.Parent == Character and not isAttacking then
isAttacking = true
stopAnimations() -- Stop all animations before attack
Attack:Play()
Attack.Stopped:Connect(function()
isAttacking = false
-- Ensure Idle animation plays if standing still after attack
if not isWalking then
Idle:Play() -- Play Idle animation after attack if not walking
end
end)
end
end
Humanoid.Running:Connect(onRunning)
Humanoid.Jumping:Connect(onJumping)
Sword.Equipped:Connect(onEquipped)
Sword.Unequipped:Connect(onUnequipped)
Sword.Activated:Connect(onAttack)
if Player.Character or Player.CharacterAdded:Wait() then
stopAnimations()
setupAnimations()
if Sword.Parent == Character then
onEquipped()
end
end
Key Changes Made
- Adjusted the logic in the
onAttack
function to ensure the idle animation is played correctly after an attack when the player is not walking.
Usage Example
To utilize the script above, simply place it in a LocalScript attached to a sword object within your Roblox game. Ensure that the sword has appropriate animations set in ReplicatedStorage
.
- Equip the sword to engage animations.
- Move your character to see the walking animation.
- Jump to trigger the jumping animation.
- Click to attack and observe that it returns to idle if not moving after the attack.
This script offers a robust framework for character animation within Roblox, showcasing best practices in Lua programming and game development. For further enhancement of skills, consider exploring courses on the Enterprise DNA Platform for data and game programming.
Description
This Lua script manages character animations for a sword-wielding character in Roblox, handling setup, equipping, unequipping, and player actions like running, jumping, and attacking, ensuring smooth transitions between animations.