Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to animate viewmodels?

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

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!

3 comments

0

It might work, but "humanoid" and "(anim)" in line 13 are marked as unknown

0

Define humanoid, I forgot to do that.

0

Ok, so I'll do local humanoid=game.players.LocalPlayer:findfirstchild"humanoid"?

This discussion is preserved as it was. New answers, comments, and votes are closed.