Help with managing different states for combat

So for a combat system I’ve been using many boolvalues to manage different states the player would currently be in like this: (Located under StarterCharacterScripts)

The reason I did this, is because I want the player to do some things only under specific conditions and I need them accessible for different scripts so I thought that would be the easiest way to handle this. But just recently I’ve realized that this isn’t really a good solution since apparently it’s not really save and there are better ways to deal with this.

What I need help with now is that I can’t really find a suitable solution to manage these states. I came up with different options but I’m not really sure in which direction to go, so I’ll list some of these here including my doubts about them.

Option 1:
Having a seperate module in ServerScriptService that saves these states and handles them like this:

local StateManager = {}

	function StateManager:addState(player, state)
		states[state][player] = true
	end

	function StateManager:removeState(player, state)
		states[state][player] = nil
	end

	function StateManager:setState(player, state, value)
		states[state][player] = value
	end

	function StateManager:returnState(player, state)
		return states[state][player]
	end

return StateManager
local States = {
	
	["Stunned"] = {	};
	
	["KnockBack"] = {};
	
	["Grounded"] = {};
	
	["Blocking"] = {};
	
	["Dodging"] = {};
...
}

return States

The thing here is I need some of these for the client too to check if the player is allowed to perform a certain move so I’d need to use remoteFunctions to get the desired state but the input would feel slow.

Option 2:
The same as Option 1 but the module is located in ReplicatedStorage so the client and server can easily get and change the states they need. Here my question is, if exploiters would have access to that and if that would be save?

Option 3
Seperating certain states I only need for the client in a seperate module than the ones I only need for the server but there would be still some I need for both…

Maybe all the option I just suggested are also bad and there are much better ways to solve this or maybe I’m also overthinking this all too much and don’t need most of these, which is a bad habit of mine, I’d really be glad if you guys could help me with this cuz it’s been bugging me for a while :smile:

1 Like

I have never made a combat system before, so if anyone sees something wrong in my comment or has better suggestions, feel free to reply.

You can have your character state controller on the client. It can handle character animations, visuals, and informing the server of which state the client is in. The server maintains a copy of the client’s state, requests certain state changes from the client, informs others of changes in the client’s state, and handles everything else not specifically assigned to the client (such as damage). The server will need to implement sanity checks to ensure the client is not engaging in suspicious activity. (For instance, sending mutually exclusive states like being downed and grabbing someone else, moving fast while in a stunned state, dealing damage while recovering, exiting and entering states at improper times or too quickly, etc.)

1 Like

Makes sense like that I’ll try that out thx

I’ve tried this through many, many, different versions of a state system. It always comes down to one fundamental problem: the server might change the client’s state before the client’s updated state signal reaches the server.

2 Likes