How do you play an animation while player is holding a tool?
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:
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() |
3 | local anim = player.Character.Humanoid:LoadAnimation(script:WaitForChild( "DrinkAnim" )) |
4 | script.Parent.Equipped:connect( function (mouse) |
5 | mouse.Button 1 Down:connect( function () |
6 | print ( "click" , player.Name) |
7 | if anim:Play() then print ( "it worked" ) end |
Here is the script for the water dispenser
01 | game.Workspace [ "Water Dispenser Model" ] .Touch.Touched:connect( function (otherPart) |
02 | if otherPart.Parent:FindFirstChild( "Humanoid" ) then |
03 | local playerName = otherPart.Parent.Name |
04 | print (playerName, " touched the Water Dispenser" ) |
05 | if not game.Players [ playerName ] .Backpack:FindFirstChild( "Cup" ) and not otherPart.Parent:FindFirstChild( "Cup" ) then |
06 | local cupTool = game.ReplicatedStorage.Tools.Cup:Clone() |
07 | local rightHand = otherPart.Parent.RightHand |
08 | local connection = Instance.new( "Motor6D" , rightHand) |
09 | connection.Name = "HandleMotor6D" |
10 | cupTool.Parent = game.Players [ playerName ] .Backpack |
11 | connection.Part 0 = rightHand |
12 | connection.Part 1 = otherPart.Parent:WaitForChild( "Cup" ):WaitForChild( "Handle" ) |
Lastly, although it may be unnecessary, here is the script for proof that the tool isn't causing problems by itself:
01 | local toolLoad 1 = script.Parent:WaitForChild( "Handle" ) |
02 | local toolLoad 2 = script.Parent:WaitForChild( "Water" ) |
03 | if toolLoad 1 and toolLoad 2 then |
04 | warn( "Cup tool LOADED" ) |
05 | script.Parent.Grip = CFrame.Angles( 0 , 0 ,math.pi/ 2 ) |
06 | script.Parent.GripPos = Vector 3. new( 0 , 0.4 , 0.3 ) |
07 | local cupWeld = Instance.new( "Weld" , script.Parent) |
08 | cupWeld.Part 0 = script.Parent.Handle |
09 | cupWeld.Part 1 = script.Parent.Water |
10 | cupWeld.C 1 = CFrame.new(- 0.1 , 0 , 0 ) |
12 | warn( "Cup tool FAILED TO LOAD" ) |