Local script in a tool, which is in the starter pack:
local players = game:GetService('Players')
local plr = players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local tool = script.Parent
-- Ensure that the character's humanoid contains an "Animator" object
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- Create a new "Animation" instance and assign an animation asset ID
local myAnimation = Instance.new("Animation")
myAnimation.AnimationId = "rbxassetid://11821593994"
-- Load the animation onto the animator
local animationTrack = animator:LoadAnimation(myAnimation)
tool.Activated:Connect(function()
animationTrack:Play()
-- Get the position and direction of the raycast
local hrp = char:WaitForChild('HumanoidRootPart')
local rayOrigin = hrp.Position
local rayDirection = hrp.CFrame.LookVector
-- Set up the raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent:GetChildren(), char:GetChildren(), workspace:WaitForChild('Baseplate')}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
-- Perform the raycast
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * 10 , raycastParams)
-- Check the result of the raycast
if raycastResult then
local hit = raycastResult.Instance
if hit.Parent:FindFirstChild('Humanoid') then
game.ReplicatedStorage.swordevent:FireServer()
end
else
warn("No raycast result!")
end
end)
There are a few potential issues that could be causing the animation to not play:
The animation asset ID is incorrect or the animation does not exist. Make sure that the asset ID for the animation is correct and that the animation asset is published and available in the game.
The animation is not loaded onto the animator properly. Make sure that the LoadAnimation method is being called correctly and that the returned animationTrack object is not nil .
The animationTrack:Play() method is not being called correctly. Make sure that the Play method is being called on the correct animationTrack object and that there are no syntax errors in the code.
The animation is being interrupted by another animation. If the character is already playing an animation, it may not be possible to play a new animation until the current animation has finished. You can use the Stop method to stop the current animation before playing the new one.
animator:Stop() -- stop the current animation
animationTrack:Play() -- play the new animation
The character does not have an Animator object. Make sure that the character’s humanoid contains an Animator object and that it is being accessed correctly in the code.
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
If none of these solutions solve the issue, you may want to check for other errors in the script or try debugging the code to identify the cause of the issue.