How to create module-script with anim id and keyboard key?

Hello, I hope you are very well!

In this post I would like to improve my learning, I would like to know if someone could give me a hand, what I need to do is create a module-script which has the id animations and the keyboard key to press to reproduce these animations, some of them could you lend a hand in doing this?

It would be great if someone could share commented code with me, it would really be very useful, thank you very much in advance!

I would really appreciate it very much, I want to improve my learning and know how to do this.

In short: have animations id and keyboard keys in a module script and connect it to a server script (normal script server).

Example:

Module script:

  • keyboard key
  • animation id

script:

  • reproduce animations pressing a keyboard key.

here is an example


local data_holder = {

"AnimationID" = {123456,3334545};
"KeyPress" = 
     "Punch" = Enum.KeyCode.K;
     "Kick" =  Enum.KeyCode.R;
}



return data_holder
2 Likes

Great thank you very much! but what I do not understand is how I can do the rest in the server script, that part I do not understand well… :frowning:

Hm Module Scripts :thinking: I don’t know much on what they do but I’ll try my best

Module Scripts can be handy for organizing your code into 1 main script that handles all of the functions & variables, rather than just having multiple regular Scripts

I’m not exactly sure if you can call a keybind from a Server Script, but you are able to on a Local Script

Both would be replicated either way regardless I’d assume

Anyways, the first thing we can do is go ahead & create our ModuleScript in workspace:

local Module = {}

return Module

Pretty simple, nothing special about it for now :thinking: Now let’s say we add a couple of our variables to our module to detect what keys & animations we want to search for:

local Module = {
    ["KeysToPress"] = {Enum.KeyCode.R, Enum.KeyCode.K, Enum.KeyCode.P};
    ["Animations"] = {"rbxassetid://00000", "rbxassetid://11111"}
}

return Module

We’ve created a Dictionary inside our Module, which would then return it’s values in case we want to use it in a different sort of script

Now the next thing we can do is go ahead & require our ModuleScript inside a LocalScript in StarterPlayerScripts:

local Module = require(workspace:WaitForChild("ModuleScript"))

print(Module)

Now I’m assuming, that this LocalScript should be a script inside a Tool object :thinking: Now what if we want to obtain 1 of its animations? Since we printed the Module, it should’ve returned back a Dictionary of the same things we first inserted in our ModuleScript

Keeping this thought in mind…

local Module = require(workspace:WaitForChild("ModuleScript"))
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")

local Animation = Instance.new("Animation")
print(Module)

Tool.Activated:Connect(function()
    local RandomID = Module.Animations[math.random(1, #Module.Animations)] --This would pick a random animation to play out of the Animations table
    print(RandomID)

    Animation.AnimationID = RandomID

    local PlayAnimation = Animator:LoadAnimation(Animation)
    PlayAnimation:Play()
end)

This would pick a random animation upon activating the Tool, then play it which should hopefully result back well!

But let’s say we want to detect a keybind, now how would we do that? Well, we can use UserInputService & check if the Key pressed is equal to what animation to want to play:

local UIS = game:GetService("UserInputService")
local Module = require(workspace:WaitForChild("ModuleScript"))

local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")

print(Module)

local Animation1 = Instance.new("Animation")
Animation1.AnimationID = Module.Animations[1]

UIS.InputBegan:Connect(function(Input, Chatted)
    if Chatted then
        return 
    end

    if Input.KeyCode == Module.KeysToPress[1] then --This would be the "R" Key as referenced back in our ModuleScript
        local PlayAnimation = Animator:LoadAnimation(Animation)
        PlayAnimation:Play()
    end
end)

This time, we’re checking if we’re hitting the “R” key or not, then if it is then we can play that specific animation!

(I believe that’s how ModuleScripts work anyways but if you have any questions do feel free to ask)

3 Likes

for a key code to work you must use the Enum line in your code

like how @JackscarIitt or @foundanerror set an example

1 Like

Thanks you a lot guys!! :smiley: This has helped me a lot in my learning

1 Like

d’aw thanks a lot

–Studking

:coefficients: