Hello! I need help with module script. How do I separate the skill script to module script?
here is my local script
local uis = game:GetService("UserInputService")
local clapAni = script:WaitForChild("Clap")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character or player.CharacterAdded:wait()
local root = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local ClapAnim = hum:LoadAnimation(clapAni)
local rp = game:GetService("ReplicatedStorage")
local remote = rp:WaitForChild("clap")
local effect = rp:WaitForChild("ClapEffect")
local cd = false
local cooldown = 5
uis.InputBegan:Connect(function(hit, chat)
if chat then return end
if hit.KeyCode == Enum.KeyCode.E then
if mouse.target ~= nil then
local target = mouse.target.Parent:FindFirstChild("HumanoidRootPart") or mouse.target.Parent.Parent:FindFirstChild("HumanoidRootPart")
if target then
local withinRange = (root.Position - target.Position).magnitude
if withinRange <= 100 then
if not cd then
cd = true
ClapAnim:Play()
wait(ClapAnim.Length)
remote:FireServer(root,target,target.Position,root.Position)
print("clapping")
wait(cooldown)
cd = false
end
end
end
end
end
end)
Place the important repeated (or will be repeated) information in the
Module script which will be
The skill function what it actually does
The debounce cool down time
And the enum keycode
Here is how it could work:
Module script:
local clapAni = script:WaitForChild("Clap")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character or player.CharacterAdded:wait()
local root = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
-- make sure to replace hum on character death
--Use Character added event to replace this hum variable on death
--like so
local ClapAnim = hum:LoadAnimation(clapAni)
player.CharacterAdded:Connect(function(newChar)
char = newChar
root = newChar:WaitForChild("HumanoidRootPart")
hum = newChar:WaitForChild("Humanoid")
ClapAnim = hum:LoadAnimation(clapAni)
end)
local rp = game:GetService("ReplicatedStorage")
local remote = rp:WaitForChild("clap")
local effect = rp:WaitForChild("ClapEffect")
local SkillsModule = {
[Enum.KeyCode.E] = {
Cooldown = 5;
Debounce = false;
OnActivate = function(self)
if mouse.target ~= nil then
local target = mouse.target.Parent:FindFirstChild("HumanoidRootPart") or mouse.target.Parent.Parent:FindFirstChild("HumanoidRootPart")
if target then
local withinRange = (root.Position - target.Position).magnitude
if withinRange <= 100 and not self.Debounce then
self.Debounce = true
ClapAnim:Play()
wait(ClapAnim.Length)
remote:FireServer(root,target,target.Position,root.Position)
print("clapping")
wait(self.CoolDown) -- access 5 in the table
self.Debounce = false
end
end
end
end,
}
}
return SkillsModule
Local script:
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(hit, chat)
if chat then return end
local skillFunction = SkillsModule[hit.KeyCode] --access table with skill data
if skillFunction then
skillFunction:OnActivate() --use
end
end)