#How do I make a animation play when i use the Left mouse button?
1 messages · Page 1 of 1 (latest)
im not sure how optimal it is since im kind of a beginner too, but this about how id do it
local inputService = game:GetService("UserInputService") -- get the input service
local character = script.Parent -- meaning the script is inside the characters model
local animator = character:WaitForChild("Humanoid"):WaitForChild("Animator") -- the object you will need to play the anim on
local newAnimationObject = Instance.new("Animation")
local animationID = "rbxassetid://YOUR_ID" -- your ID
newAnimationObject.AnimationId = animationID -- set the animation ID
newAnimationObject.Parent = script -- parents the newly created "Animation" object to the script
local loadedAnim = animator:LoadAnimation(script:WaitForChild("Animation")) -- load the animation, pretty sure it's better to do it outside the function
loadedAnim.Looped = false
local animPlaying = false -- a value like what you used
local function playAnimation() -- a function we can call by doing "playAnimation()", anything inside plays
if script:FindFirstChild("Animation") and not animPlaying then
-- if the animation object is found AND animPlaying is false (adding not checks for a false return while "... and animPlaying" checks for a true return)
animPlaying = true -- set to true to avoid the function running twice
print("Animation object found", script.Animation) -- puts a msg in the output for debugging)
loadedAnim:Play() -- play the animation that loaded in line 4
else
warn("Animation object not found")
end
animPlaying = false -- resets the value for the next valid use
end
game.Players.LocalPlayer:GetMouse().Button1Down:Connect(playAnimation) -- call the function when the player clicks
i tried to explain it and i tested it w/ my own anim
Thanks!
** You are now Level 1! **
Wtf is this
Why don't u just make the animation manually and set it's id and name, the code would look much cleaner
Humanoid:LOadAnimation is deprecated
** You are now Level 1! **
Nvm