How to Make a Plug-N-Play Power System Using Knit

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
image
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.

2 Likes

I might be outdated on my Knit knowledge as I only used it a bit some months ago, but isn’t the .Client part of a Knit Service supposed to be the part that is accessible to the client through a kind of Client to Server invoke?

Wouldn’t that make it kind of insecure against exploits? Though there are not many of them in the platform anymore I think.

I might be wrong, if so I’d appreciate if you could explain it better to me.

1 Like

Yes, I didn’t take into consideration exploits or anything of sorts, I just wanted to show like a template. But if somehow they can get the name of the Knit service I assume they’d be able to exploit it.