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

How do you play an animation while player is holding a tool?

Asked by 5 years ago

My goal is to have a water dispenser give a player a cup of water when touched and have an animation of them drinking from it play when a click is heard and the tool is equipped. I have encountered multiple forums answering this question, and I have done as much as getting the mouse input, loading the animation, and attempting to play it.

I also made sure that there is a Motor6D with Part0 as the RightHand and Part1 as the Handle of the tool.

https://www.dropbox.com/s/1ni52fn67mup3n4/Screenshot_25.png?dl=0

I have tried to troubleshoot the script by making it print to console in cases such as if a click was heard as well as then the animation was successfully started. The "click" was printed to the console each time on every click, but the text for when the animation should have run did not print. Just to be sure, I got rid of the "if" statement and left it as anim:Play(), with no luck.

Here is the animation local script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local anim = player.Character.Humanoid:LoadAnimation(script:WaitForChild("DrinkAnim"))
script.Parent.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        print("click", player.Name)
        if anim:Play() then print("it worked") end
    end)
end)

Here is the script for the water dispenser

game.Workspace["Water Dispenser Model"].Touch.Touched:connect(function(otherPart)                                               
    if otherPart.Parent:FindFirstChild("Humanoid") then                                                                         
        local playerName = otherPart.Parent.Name                                                                                
        print(playerName, " touched the Water Dispenser")                                                                       
        if not game.Players[playerName].Backpack:FindFirstChild("Cup") and not otherPart.Parent:FindFirstChild("Cup") then
            local cupTool = game.ReplicatedStorage.Tools.Cup:Clone()
            local rightHand = otherPart.Parent.RightHand
            local connection = Instance.new("Motor6D", rightHand)   
            connection.Name = "HandleMotor6D"
            cupTool.Parent = game.Players[playerName].Backpack
            connection.Part0 = rightHand
            connection.Part1 = otherPart.Parent:WaitForChild("Cup"):WaitForChild("Handle")
        end
    end
end)

Lastly, although it may be unnecessary, here is the script for proof that the tool isn't causing problems by itself:

local toolLoad1 = script.Parent:WaitForChild("Handle")
local toolLoad2 = script.Parent:WaitForChild("Water")
if toolLoad1 and toolLoad2 then
    warn("Cup tool LOADED")
    script.Parent.Grip = CFrame.Angles(0,0,math.pi/2)
    script.Parent.GripPos = Vector3.new(0,0.4,0.3)
    local cupWeld = Instance.new("Weld", script.Parent)
    cupWeld.Part0 = script.Parent.Handle
    cupWeld.Part1 = script.Parent.Water
    cupWeld.C1 = CFrame.new(-0.1,0,0)
else
    warn("Cup tool FAILED TO LOAD")
end

2 answers

Log in to vote
1
Answered by 5 years ago

I now understand what happened.

I recreated the animation to work with the Handle and added a head tilt, and the head was able to tilt. However, the player's right arm would not move.

I did some more research, specifically based on that the right arm wouldn't move, but I now see the issue: the priority level was set to Core, as oppose to Action.

In the Animation Editor plugin, there is an option saying "Set Priority Level," which should be set at "Core" by default. To perform an animation while holding a tool, use "Action" as the priority level.

My scripts were successful, so feel free to use them in anyway you'd like. This has been quite the learning experience!

Ad
Log in to vote
0
Answered by
Avoxea 48
5 years ago

This is the script I use to load animations. It needs to be a local script inserted into the tool. You also need the Animation object, also inserted into the tool.

debounce = false
script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        if debounce == false then
            debounce = true
            animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
            animation:Play()
            wait(1.5)
            debounce = false
        end
    end)
end)

script.Parent.Unequipped:Connect(function()
        animation:Stop()
end)

If you have already animated an animation, just copy and paste the ID into the animation object. If it can't access it, then it might be because you're trying to use an animation that's not yours. You have to make the animation yourself or use one from your Team Animations if you're scripting a group game.

0
The animation script is parented over the animation with the right ID. I'm wondering if the fact that the tool keeps moving between the character model and the player's backpack is a conflict. Also, I had something missing in my code before, which allowed the animation to play when the animation wasn't equipped, so it should be a valid animation. ThEJuDgE1 5 — 5y

Answer this question