Play animation without a tool

Hey!
I am trying to make a script that when a player clicks, an animation plays:

Here is the script:

local player = game.Players.LocalPlayer
local Character = player.Character
local Humanoid = Character.Humanoid
local mouse = player:GetMouse()
local AnimationId = "rbxassestid://04819508252"

mouse.MouseButton1Click:Connect(function()
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId
local LoadAnimation = Humanoid:LoadAnimation(Animation)
Animation:Play()
wait(1)
Animation:Destroy()
end)

It is inside StarterPlayerScripts, but it does not work. How can I overcome this?

1 Like

sorry for the amount of Scripting Support posts - I am making a game and these are vital!

1 Like
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local AnimationId = "rbxassestid://04819508252"

mouse.MouseButton1Click:Connect(function()
local Humanoid = player.Character.Humanoid
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId
local LoadAnimation = Humanoid:LoadAnimation(Animation)
Animation:Play()
wait(1)
Animation:Destroy()
end)

Try this.

That doesn’t work either. Hmmmm

Replace ‘MouseButton1Click’ with ‘MouseButton1Down’.

First of all, I recommend putting this script either in StarterPack or StarterGui but NOT in StarterPlayerScripts because once your character respawns, the variable “Character” is set to nil and your whole script isn’t functional anymore.

Also, make sure the character is properly loaded before firing the mouse click event by using :wait().

--< Variables
local PlayerService = game:GetService('Players')
local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
1 Like

Hmmm, still doesnt work. What shall i do?

The scripts above should work. Check console (F9) and see if it says it can’t load. If you don’t own the animation then it won’t work. If the animation is made by you, but the game is on your group, then you must reupload your animation to be owned within your group.

1 Like

Are you getting errors or is the script running and just not responding or behaving as expected, is the script even running at all? You have to provide more details other than “this isn’t working.” The first thing that comes to mind is you are likely trying to use it in a ServerScript(the “Script” object) instead of a LocalScript. You need to use a LocalScript in order to play animations on the client and even reference the LocalPlayer. If you are using this, is the script located in the correct place? The LocalScript can’t run on the server because then there is no player client that it can interact with. The LocalScript must be in a folder that is replicated to the client(ReplicatedStorage, ReplicatedFirst, StarterPlayerScripts, StartGui, etc.). If both of these are true, then you should debug your code. Is it erroring? Is it stuck in a loop? Is the event even firing? Is the script even getting to the point where my code should execute? All of these are basic debugging scenarios which you should be familiar with.

The easiest way to determine where in the code the issue is occurring is by placing print statements and seeing which ones show up in the output, which ones don’t, which ones are showing up at the wrong time, too many times, etc.

If you need help from someone else, make sure you do these steps first and you try to figure out where/why your bug is happening, before just throwing some code on the forum and expecting other people to do the work for you. If you are genuinely stuck, then that is an appropriate time to ask for help. But make sure you try these steps to find the issue yourself so that you can tell someone “this is where my code isn’t working” so that they can help you better rather than taking a guess or not knowing what you need help with.

2 Likes

Slight suggestion.
I’d use UserInputService with the InputBegan event instead of LocalPlayer:GetMouse()
Example of UserInputService being used

-- We must get the UserInputService before we can use it
local UserInputService = game:GetService("UserInputService")
 
-- A sample function providing one usage of InputBegan
local function onInputBegan(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been pressed!")
	end
end
 
UserInputService.InputBegan:Connect(onInputBegan)

Output says:
22:39:48.389 - MouseButton1Click is not a valid member of PlayerMouse
In the console, it says: “assetId” could not be loaded.

script is a local script, in StarterGui.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local AnimationId = "http://www.roblox.com/asset/?id= " -- use your animation id

mouse.Button1Down:connect(function()
local Humanoid = player.Character.Humanoid
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId
local LoadAnimation = Humanoid:LoadAnimation(Animation)
LoadAnimation:Play()
wait(1)
Animation:Destroy()
end)

This should work, just replace the AnimationId and use “http://www.roblox.com/asset/?id=” instead of “rbxasset://”.

2 Likes

Thanks so much for the help!
I really appreciate it!

It worked!

1 Like