Hm Module Scripts
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
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
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)