I want to improve the code by adding an module script, what do you agree and dislike about this newly written code?
-- // Variables // --
local UserInputService = game:GetService('UserInputService')
local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local Key = 'C'
-- // Crouch Animation // --
local AnimationBegan = Instance.new('Animation')
-- // Functions // --
local function InsertId(Object, Id)
if (Object:IsA('Animation')) then
Object.AnimationId = 'rbxassetid://' .. Id
end
end
InsertId(AnimationBegan, 04846138403)
-- // Main Script & Execution // --
UserInputService.InputBegan:Connect(function(inputGiven)
if inputGiven.KeyCode == Enum.KeyCode[Key] then
local LoadAnimation = Player.Character.Humanoid:LoadAnimation(AnimationBegan)
LoadAnimation:Play()
end
end)
For the event InputBegan, there is an argument named “gameProcessedEvent”, checking this to make sure it is false before executing the keybind this means the crouch will not fire if the player is typing in chat, also when they are typing in boxes and other game-related input. For more detail you can check the wiki article, just make sure you are referring to the UserInputService section.