State Manager for game

Hello I am trying to create a state manager that detects whether a player is attacking, blocking, rolling, etc.? What is the most optimal approach? I’ve heard attributes can be effective, but they can also be performance-intensive. Any ideas?

Any help will be appreciated!

1 Like

A string Value called State inside the player, then you can set the value to attacking, blocking, e.t.c

1 Like

I do not recommend that, because what if the character dies while blocking? Then he spawns while blocking, because it is inside the player! Therefore I recommend making the value inside character, rather than inside player instance.

2 Likes

Attributes aren’t that performance intensive, unless you have like a 10.000 attributes!
I recommend sticking to using attributes, that are of the character (not the player), and using Attribute.AttributeChanged events to check if the attribute changed and do something with that. This is the best approach, because it is event-based and doesn’t require a loop! This is the simplest and best for performance way, therefore the best way in my view.

3 Likes

like DevHelloBack said attributes Arnot performance intensive in fact they are a lot faster than instance values
Attributes vs Value, which is better? - Resources / Community Tutorials - Developer Forum | Roblox

2 Likes

Set the state to Idle or nil on death? But probably making it in the character is better, I just put player since its faster to write, but oh well.

3 Likes

The attribute is automatically deleted on death, because the character itself is deleted when you respawn. So you don’t need to do anything. A script should check when a character is added and puts the attribute to the character.

2 Likes

Same thing with all the other instances in the character? And the values? But I get your point.

3 Likes

No, only give the character model the attribute.

2 Likes

I wasn’t asking a question lol, I was just saying, but sure, attributes might be better, I just use values because it’s just easier for me to understand what’s happening, and I can actually see the instance, rather than checking for attributes in the instance.

1 Like

100% use attributes, they are much faster than creating instances.

3 Likes