How to Make a Plug-N-Play Power System Using Knit
You may need other scripts like a Knit Runtime to load certain services
Client Side
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local moveDebounce = false
local PowerInfo = game:GetService("ReplicatedStorage").PowerInfo
Knit.OnStart():andThen(function()
local PowerSaveService = Knit.GetService("PowerSaveService")
local powerFolder
PowerSaveService:RetrivePower():andThen(function(power)
powerFolder = PowerInfo:FindFirstChild(power)
local PowerService = Knit.GetService(power .. "PowerService")
for i,v in ipairs(powerFolder:GetChildren()) do
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode[v.Keybind.Value] and moveDebounce == false then
moveDebounce = true
PowerService[v.Name](player):andThen(function()
moveDebounce = false
end)
end
end)
end
end)
end):catch(warn)
This is a pretty basic power system with no cooldowns or anything but it provides a pnp environment
At first, it retrieves the power save service which is a datastore for what power the player has. The power folder is any of these
In each of the folders resides the power name and a key bind for said power. You can easily add more powers to this folder which makes in pnp. Then the code checks for each key bind if itâs pressed.
Server Sided Power Storage
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local PowerSaveService = Knit.CreateService { Name = "PowerSaveService", Client = {}}
local DSS = game:GetService("DataStoreService")
local PowerStore = DSS:GetDataStore("PowerStore")
-- UserID | Power
function PowerSaveService.Client:RetrivePower(player)
return self.Server:RetrivePower(player)
end
function PowerSaveService:RetrivePower(player)
local success, power = pcall(function()
return PowerStore:GetAsync(player.UserId)
end)
if success then
return power
end
end
function PowerSaveService.Client:SetPower(player, power)
local success, err = pcall(function()
return PowerStore:SetAsync(player.UserId, power)
end)
if not success then
print(err)
end
end
return PowerSaveService
This is just a Knit service, each function is pretty self-explanatory
Example of Power
local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)
local LightningPowerService = Knit.CreateService { Name = "LightningPowerService", Client = {}}
function LightningPowerService.Client:Stomp(player)
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. tostring()
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
wait(0.2)
for i, v in ipairs(game:GetService("Players"):GetChildren()) do
if v.Name ~= player.Name and (character.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).Magnitude >= 20 then
v.Character.Humanoid:TakeDamage(20)
end
end
end
function LightningPowerService:KnitInit()
print("Lightning Power Service has initializied")
end
return LightningPowerService
This is a really simple power, but the premise is that you make functions that are the same name as in the folder, the handler runs the functions and therefore the power. You can code whatever kind of power youâd like.