Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my animated Pike/ Spear not do its animation after being put inside the StarterPack?

Asked by 3 years ago
Edited by imKirda 3 years ago

My Spear is animated and when its just in the workspace, i can pick it up and it does it's animation when it is clicked. However, i'm trying to put it in the StarterPack and it's not letting me do the animation. Here is the local script i used to play the animation:

local player = game:GetService("Players").LocalPlayer
local character = player.character
local animation = script.Parent:FindFirstChild("Stab")
local swingAnimation = character.Humanoid:LoadAnimation(animation)

local canswing = true

local debounce = 1

script.Parent.Activated:Connect(function()
    if canswing then 
        canswing = false
        swingAnimation:Play()
        wait(debounce)
        canswing = true
    end
end)

Blue Lua icon in the editor creates code block, put code inside of it!!!!

Also, the error message says: Players.LegoDan0101.Backpack.Rusty Spear.LocalScript:4: attempt to index nil with 'Humanoid'

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Theres your answer.

-- Alright heres an answer

local player = game:GetService("Players").LocalPlayer
local character = player.character
local animation = script.Parent:FindFirstChild("Stab")
local swingAnimation = character.Humanoid:LoadAnimation(Animation) -- here mispelling (fixed)

local canswing = true

local debounce = 1

script.Parent.Activated:Connect(function()
    if canswing then 
        canswing = false
        swingAnimation:Play()
        wait(debounce)
        canswing = true
    end
end)
0
This shows the exact same error message for me but thanks for trying. LegoDan0101 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

I'm pretty sure that LoadAnimation is deprecated. If it helps, try using animator.

read up here: https://developer.roblox.com/en-us/api-reference/class/Animator

local player = game:GetService("Players").LocalPlayer
local character = player.character
local animation = script.Parent:FindFirstChild("Stab")
local animator = character.Humanoid.Animator -- gets the animator of the humanoid
local animationtrack = animator:LoadAnimation(animation) -- loads the animation

local canswing = true

local debounce = 1

script.Parent.Activated:Connect(function()
    if canswing then
        canswing = false
        animationtrack:Play() -- plays the animation
            wait(debounce)
            canswing = true
    end
end)

0
This shows the exact same error message for me but thanks for trying. LegoDan0101 0 — 3y
Log in to vote
0
Answered by 3 years ago

I have worked it out, for anyone else with the same problem, here was my final code for this.

wait ()
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local character = game.Players.LocalPlayer.Character
local animation = script.Parent:FindFirstChild("Stab")
local humanoid = character:FindFirstChildWhichIsA("humanoid")
local player = humanoid and game.Players:GetPlayerFromCharacter(humanoid.Parent)
local swingAnimation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
local animator = character.Humanoid.Animator

local canswing = true

local debounce = 1

script.Parent.Activated:Connect(function()
    if canswing then
        canswing = false
          swingAnimation:Play()
       wait(debounce)
       canswing = true
    end
end)

local pl = workspace:FindFirstChild("Player")
if pl then
    local hum = pl:FindFirstChild("Humanoid")
    if hum then
        hum.Health = 0
    end
end

Answer this question