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

Animation Won't Play When The Mouse Clicks?

Asked by 3 years ago

So I am creating a simulator and most people know that when you are holding the tool or weapon and you click it plays and animation and gives strength. I am not focusing on the strength yet but I just need help on the animation. So the tool I am using is a wand and when you are holding it and you click, the animation will play. Everything is fine except for the animation. The animation won't play. The AnimationId is correct and and the animation is not nil. Everything but the "AnimationIsNil" is printing. I have moved some lines to other places and it still does not work. If any of you have a solution please tell me.

This is a local script in the StarterPlayerScripts

local mouse = game.Players.LocalPlayer:GetMouse()
local Character = game.Players.LocalPlayer.Character
if not Character or not Character.Parent then
    Character = game.Players.LocalPlayer.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local WandMovement = Instance.new("Animation")
WandMovement.AnimationId = "rbxassetid://6602046971"
local Tool = game.Players.LocalPlayer.Backpack:FindFirstChildWhichIsA("Tool")

Tool.Equipped:Connect(function()
    print("ToolEquippedFUnction")
    mouse.Button1Down:Connect(function()
        print("IntoMouseButtonFunction")
        local WandMovementAnimationTrack = Animator:LoadAnimation(WandMovement)
        WandMovementAnimationTrack:Play()
        if WandMovementAnimationTrack == nil then
            print("animationtrackisnil")
        else
            print("AnimationIsNotNil")
        end
    end)
end)

Thank you for reading!

0
Also the priority of the Animation is Action MarcTheRubixQb 153 — 3y

2 answers

Log in to vote
1
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

It will only play the animation when you equip and click at the same frame since it only fires the mouse click function when Tool equipped event is fired. So do this instead:

debounce = false
local Character = game.Players.LocalPlayer.Character
if not Character or not Character.Parent then
    Character = game.Players.LocalPlayer.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local WandMovement = Instance.new("Animation")
WandMovement.AnimationId = "rbxassetid://6602046971"
local Tool = game.Players.LocalPlayer.Backpack:FindFirstChildWhichIsA("Tool")

Mouse.Button1Down:Connect(function()
    if debounce = false then
        debounce = true
        if Character:FindFirstChild(""..Tool.Name) then
            print("IntoMouseButtonFunction")
            local WandMovementAnimTrack = Animator:LoadAnimation(WandMovement)
            WandMovementAnimTrack:Play()
            if WandMovementAnimTrack == nil then
                print("animationtrackisnil")
            else
                print("AnimationIsNotNil")
            end
        end
    else
        return
    end
end)

Mouse.Button1Up:Connect(function()
    wait()
    debounce = false
end)

I added debounce (cooldown) in case that was the problem.

0
Sorry, but this script sadly did not work. Thank you for trying to help me though! :) MarcTheRubixQb 153 — 3y
0
oof ok NGC4637 602 — 3y
0
OH MY GOD IM AN IDIOT I mispelled debounce to denounce lmao NGC4637 602 — 3y
0
there i fixed the typo NGC4637 602 — 3y
0
Ok I just figured out I needed to add one more thing and it worked! Thank you! MarcTheRubixQb 153 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Instead of

Animator:LoadAnimation(WandMovement)

you should try doing

Humanoid:LoadAnimation(WandMovement)

If this doesn't work, try changing the animation priority. I hope this helped!

0
I have tested it but it still has not work. Thank you leaving an answer to help me! :) MarcTheRubixQb 153 — 3y
0
Humanoid:LoadAnimation is deprecated. Animator:LoadAnimation is correct NGC4637 602 — 3y

Answer this question