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

Animation after pressing a key doesn't work?

Asked by 6 years ago

I have two scripts: Controls, and Animation.

Controls:

Mouse = game.Players.LocalPlayer:GetMouse()

--Punching
Mouse.KeyDown:connect(function(key)
    if key == "q" then
        game.ReplicatedStorage.RemoteEvent:FireServer(01900686745)
    end
end)

Animation

--Punching
game.ReplicatedStorage.OnServerEvent:Connect(function(player, animationID)
    local animation = Instance.new("Animation")
    animation.AnimationID = "http://www.roblox.com/Asset?ID"..animationID

    local loadedAnimation = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
    loadedAnimation:Play()
end)

When I test the game and press q, this error occurs: Plugin_142731176._Char:93: attempt to index global 'character' (a nil value)

Any ideas on how to fix this?

2 answers

Log in to vote
0
Answered by
popeeyy 493 Moderation Voter
6 years ago

There are multiple error in your code.

First of all, you're getting OnServerEvent on ReplicatedStorage, not a RemoteEvent. In my revised working code below, I created a Remote Event in ReplicatedStorage called "punchingEvent".

Also, you were trying to get "KeyDown" on a Mouse, not a keyboard. You should use UserInputService for this, and use "InputBegan". When the input begins, you can check if it was a key, and what key they pressed. Then, the "q" should be Enum.KeyCode.Q because the KeyCode is an Enum.

Lastly, AnimationID isn't a valid member of animation, it's AnimationId. It's case sensitive.

FYI: You can get character from the player object, you don't have to find it in workspace.

That error you received was from a plugin you had installed, not your code, unless your code is a plugin.

It gave me an error because I don't own the animation, but I hope you do so you can run it!

Here's the revised code:

Animation (ServerScriptService) Script

game.ReplicatedStorage.punchingEvent.OnServerEvent:Connect(function(player, animationID)
    local animation = Instance.new("Animation")
    animation.AnimationId = "http://www.roblox.com/Asset?ID"..animationID

    local loadedAnimation = player.Character.Humanoid:LoadAnimation(animation)
    loadedAnimation:Play()
end)

Controls (StarterGui) Local Script

local UIS = game:GetService'UserInputService'

UIS.InputBegan:connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        local keyPressed = input.KeyCode
        if keyPressed == Enum.KeyCode.Q then
            game.ReplicatedStorage.punchingEvent:FireServer(01900686745)
        end
    end
end)

I hope this helped!

0
you forgot an = after the ID but I fixed it anyway EzraNewLight 6 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

first of all running an animation serverside isn't the smartest thing and keydown is depracated

use

game:GetService("UserInputService").InputBegan:connect(function(Input, GPE)
    if Input.KeyCode == Enum.KeyCode.Q and not GPE then
        game.ReplicatedStorage.RemoteEvent:FireServer(01900686745)
    end
end)

run the animationloader in ur local script and run touchedserver side while protecting ur remotes

Answer this question