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?
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)
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()
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.
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.
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)
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)