I was trying to make an animation for my physics game. I was making a crouch animation. Can someone take a look at the script if possible.
attempt to index nil with 'GetMouse' ?
2 answers
The problem is that the script loads faster than the player, so you're trying to get the player's mouse when the player doesn't exist. However, you should use UserInputService if you want to get a key press event. There's also no reason to create the animation each time the key is pressed, instead, create it once and have it play or stop each time the key is pressed.
LocalScript:
local plr = game:GetService('Players').LocalPlayer
repeat wait() until plr and plr.Character and plr.Character:FindFirstChild('Humanoid')
local hum = plr.Character.Humanoid
local UIS = game:GetService('UserInputService')
local Animation = Instance.new('Animation')
Animation.AnimationId = 'rbxassetid://123'
local AnimTrack = Humanoid:LoadAnimation(Animation)
UIS.InputBegan:Connect(function(input,gp)
if input.KeyCode == Enum.KeyCode.C then
AnimTrack:Play()
end
end)
UIS.InputEnded:Connect(function(input,gp)
if input.KeyCode == Enum.KeyCode.C then
AnimTrack:Stop()
end
end)
You may change
mouse.KeyDown:Connect(function(Key)
to
MouseButton1Click:Connect(function(Key) --Change the 1 to 2 if you want it to right click
2 comments
that wasnt a problem in the 1st place. The error said 15:15:57.678 - Workspace.charlieisck.Script:2: attempt to index nil with 'GetMouse'
Remove the mouse local. You can't call the players mouse like you did it. You must call it with the function. So uh. Remove: local mouse = player:GetMouse()
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Don't use keydown, use UserInputService: https://developer.roblox.com/en-us/api-reference/class/UserInputService