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

can someone help me? When i press "E" i want it to play an animation.

i dont have any code because im not a scripter but im making this game by myself. but anyways i need it so when i press e it plays an animation skjkdasfhdkfhagsdjfasvjaf

2 answers

Hello!

So if you already have an animation and you exported it then you gotta create LocalScript inside of StarterPack, Script inside of Workspace and a RemoteEvent inside of ReplicatedStorage. You also gotta have an object called Animation inside of your Script you just created. Select your Animation and in the properties change AnimationId to id of your animation. You can find it in the link when you are on the animation's page.

LOCALSCRIPT CODE:

local UIS = game:GetService("UserInputService") --user input service variable
local event = game.ReplicatedStorage:FindFirstChild("RemoteEvent") --remote event variable

UIS.InputBegan:Connect(function(input, gameProcessed) --function starts when input starts
	if input.UserInputType == Enum.UserInputType.Keyboard then --checking if user is using keyboard
		if input.KeyCode == Enum.KeyCode.E then --checking if the key pressed is E
			event:FireServer() --firing event to the server
		end
	end
end)

SCRIPT CODE:

game.ReplicatedStorage:FindFirstChild("RemoteEvent").OnServerEvent:Connect(function(player) --getting remote event
	local hum = player.Character.Humanoid --humanoid variable
	local anim = script.Animation --animation variable
	local playing = hum:LoadAnimation(anim)
	playing:Play() --playing the animation
end)

When I tested it out it worked for me, if you will have any troubles, say it.

1 comment

0

It is unnecessary to use a RemoteEvent since you can play the animation on the client, and the animation will replicate to the other clients. Also, Humanoid:LoadAnimation() is deprecated, use Animator:LoadAnimation().

Put This Script In Starter Character Scripts And Make Sure This Is A Local Script AND The Most Important Thing The Animation Should Be Made By You And No One Else Or Else The Script Wont Work If The Game Is In A Group Publish The Animation Using A Group

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService('UserInputService')
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://" -- add ur animation id after the "//"
function onKeyPressed(inputObject,gameProcessEvent)
	if inputObject.KeyCode == Enum.KeyCode.E then -- you can change the key code
		Humanoid:LoadAnimation(anim):Play()
		print("Success!")
	end
end
uis.InputBegan:Connect(onKeyPressed)

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