This is my code currently sorry for no indentation
Oh, I got it.
Please respond with the solution to your problem and mark it as solution so that other people having similar issues will have the opportunity to learn.
Yeah, it irks me when I try to find a solution to an old problem after reading maybe dozens of replies just to see “I fixed it” with no explanation.
On a side note, what is it with you guys and not indenting your code? Roblox does it automatically for you.
He said he finds it hard to read.
I know that the OP has already solved his problem, but I think it’s important for someone to explain what went is wrong with the code for future reference.
PROBLEM:
Here is the following code mentioned from the OP but indented:
Mouse.Button1Down:connect(function()
local PickUpAnimObject = game.ReplicatedStorage.Animations.Hold
local PickUpAnim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(PickUpAnimObject)
if Mouse.Target ~= nil then
local CanBePickedUp = Mouse.Target:FindFirstChild("CanPickUp")
local magnitude =(game.Players.LocalPlayer.Character.HumanoidRootPart.Position - Mouse.Target).magnitude
if Holding == false then
if CanBePickedUp then
if magnitude < 7 then
game.ReplicatedStorage.Events.PickUp:FireServer(Mouse.Target)
PickUpAnim:Play()
Holding = false
end
end
end
elseif Holding == true then
game.ReplicatedStorage.Events.Throw:FireServer()
Holding = false
PickUpAnim:Stop()
end
end)
The problem is that every time you left click, you are creating a new AnimationTrack (The instance created when you load an animation).
Therefore, when you try to stop the animation, you’re only stopping the newly created animation.
One other problem that I have spotted is that you have no idea of knowing whether the user’s character is still in Workspace.
SOLUTION:
There are multiple ways we could fix this but the most basic way is to listen to the CharacterAdded event of a player (to get the character) and then load the animation into the character which will then be your new animation variable.
Here is how you’d implement this:
--GetService
local Players = game:GetService("Players")
local ReplicatedStorage = game.ReplicatedStorage
--Variables
local PickUpAnimObject = ReplicatedStorage.Animations.Hold --Location of your animation
--CharacterAdded
Players.CharacterAdded:connect(function(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local PickUpAnim = Humanoid:LoadAnimation(PickUpAnimObject)
PickUpAnim:Play()
Wait(5) --For debuging
PickUpAnim:Stop()
end)
In case that your script is inside a tool, change your implementation to this:
--GetService
local Players = game:GetService("Players")
local ReplicatedStorage = game.ReplicatedStorage
--Variables
local Tool = script.Parent --Location of your tool
local PickUpAnimObject = ReplicatedStorage.Animations.Hold --Location of your animation
local Character = Players.LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")
local PickUpAnim = Humanoid:LoadAnimation(PickUpAnimObject)
Tool.Equipped:connect(function(Mouse) --How to get the mouse from a tool | Automatically disconnected on unequipped
Mouse.Button1Down:connect(function()
PickUpAnim:Play()
end)
end)
Few side notes:
- I’ve only shown you implementation on how to do what you’re doing propperly, not the fixed version of your code.
- Please wrap your code with ``` when posting in development support so that we can help you easier, it makes our lives that much easier.
- Whether or not you plan on indenting your code, on the DevForum, it’s much easier for others to read when you’ve laid out your code in an efficient manner It’s a good practice to begin using indenting, especially since Roblox allows this behavior automatically by default.
- Also try be more descriptive when posting in Development support, that way people know what’s wrong with your code to begin with.