Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to animate viewmodels?

Asked by
Lyphios 77
4 years ago

I have a brawler game, and inside of replicated storage, I've got some fake arms that are raycasted and animated. My question is, how do I actually make it so that when I press "e" or press left click, it plays the animations? because with the script I have right now, it doesn't do anything. Here's my code, tell me where I went wrong, and what I can do instead

local player = game.ReplicatedStorage.Arms
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid//:noseeslol" 
mouse.KeyDown:connect(function(key)
    if key == "r" then 
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
    end
end)

1 answer

Log in to vote
0
Answered by
Rinextel 291 Moderation Voter
4 years ago
Edited 4 years ago

Hello!

There are a few problems with your script. First off, you never parented the animation! You created it, but never gave it a parent.

Second, this script would cause immense lag because every time a player presses "r" it creates a new Animation. Which is just not needed. You can solve your issue by doing this,

local player = game.ReplicatedStorage.Arms
local User = game:GetService("Players").LocalPlayer
repeat wait() until User.CharacterAdded
if User.Character:FindFirstChild("WhateverYouNameTheAnimation") == nil then  --Checks if the animation is already there
    local anim = Instance.new("Animation")
        anim.AnimationId = "rbxassetid//:noseeslol"
        anim.Name = "WhateverYouWantToName"
        anim.Parent = User.Character
end

local function KeyPress (input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.R then
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
    end
end

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(KeyPress)

This should work! If there are any errors then let me know! Happy scripting!

Be sure to upvote if this works and mark as solution!

0
It might work, but "humanoid" and "(anim)" in line 13 are marked as unknown Lyphios 77 — 4y
0
Define humanoid, I forgot to do that. Rinextel 291 — 4y
0
Ok, so I'll do local humanoid=game.players.LocalPlayer:findfirstchild"humanoid"? Lyphios 77 — 4y
Ad

Answer this question