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

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

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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:

01local UIS = game:GetService("UserInputService") --user input service variable
02local event = game.ReplicatedStorage:FindFirstChild("RemoteEvent") --remote event variable
03 
04UIS.InputBegan:Connect(function(input, gameProcessed) --function starts when input starts
05    if input.UserInputType == Enum.UserInputType.Keyboard then --checking if user is using keyboard
06        if input.KeyCode == Enum.KeyCode.E then --checking if the key pressed is E
07            event:FireServer() --firing event to the server
08        end
09    end
10end)

SCRIPT CODE:

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

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

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(). COUNTYL1MITS 312 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

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

Answer this question