Efficient Stun system

image

So I want to rework my current combat system to be something more efficient. There isnt anything functioning incorrectly with the current system. if the stun > 0 the npc / player is slowed. However is there a more modular way of tracking this sort of thing. NPCS and Players

1 Like

Well a “modular” way to have your code which makes your game more organized and code look nicer is either OOP or just a normal modular approach

NORMAL MODULAR APPROACH:
ServerScript

for _, v in pairs(game.ServerScriptService:GetDescendants()) do
if v:IsA("ModuleScript") then
local m = require(v)
if m.Init then
 task.spawn(m.Init)
end

–SYSTEMS:

--Get your local variables
--Segment parts of your system in functions or local functions
function System.Init()
--Code logic goes here
end

This applies for the client too
Edit: This is just how I do stuff and many others too do use this type of approach and I have to say it’s really good for organization :3

As a suggestion you can also use bindable events inside of your modules since they client to client and server - server communication so for example you can have your stun system inside of a bindable event that takes the parameter character and applies any changes to the character

So you can basically apply stun on any character player or not

so would i say store character stuns in a table like

stunTbl = {
    [plr1] = true,
    [npc] = false,
}

if i were to do that how would i trigger the states or get when they are changed. when stun is true walkspeed = 0 for example. i previously used propertyChanged with the values to detect when it changed and if over 0 then set walkspeed to x

I’m not sure by what you mean. But this is how I would go about doing it

Example:

local StunBindable = ReplicatedStorage.Events.StunBindable
local CombatValues = --GET YOUR COMBAT VALUES

StunBindable.Event:Connect(function(character)
 if character:FindFirstChild(CombatValues.Stunned) then
 character.CombatValues.Stunned = true

local Humanoid = character:FindFirstChild("Humanoid")
Humanoid.WalkSpeed  = --Set walkspeed
Humanoid.JumpPower = --SetJumpPower

ServerScript:

local StunBindable = ReplicatedStorage.Events.StunBindable
StunBindable:Fire(--A model that has a character. Can be a Player or an NPC)

This is actually quite helpful. my only concern is that it isnt updated per frame. therefore an exploiter could just change their walkspeed back to normal.

any solutions?

If you want to use a modular approach and something a little safer from exploiters (more accidental than on purpose) you should look into using ECS

Apologies for the extremely late response but I found this video where you could add your own values instead.