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:

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.

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

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)

Answer this question