States - attacking,blocking etc

I’ve been confused on this for a while and I’ve been trying to get answers on the forum but I don’t really understand them.

What’s the best way to create states like attacking and blocking, currently I’m using attributes by creating them as soon as the players character is added on a server-script, and it’s getting annoying when I have to check them individually everytime using if statements.

I heard that module scripts are a good substitution but how would I check for another players state.

1 Like

honestly, this isnt the best answer, but tables.

1 Like

You’d have to check if a player has a certain state either way. Attributes work fine in my opinion

2 Likes

Alright thanks for the help (30charlimit)

1 Like

Do you mind expanding on this, how would I use these tables

1 Like

Probably your best bet is attributes on the character, if you really wanted too you could use module scripts since they can be accesed from any script.

1 Like

Tables in lua(u) are crucial and used everywhere. They are basically containers that hold values. In your case you would put all states of every charcter in the game inna table.

Like regular variables you would assign a table using “local” follow by a name of your choice.

Then you would use the brackets: {}. And now this is your standard table. Think of these brackets as the wall and anything inside these walls are part of the table.

Example:

local table = {}

Now when you put something in a table there are 2 components it has. A key and a value.

So if i were to put a value inside the table it would be like this:

local table = {state = “blocking”}
The key is state and the value is “blocking”

But you would have to manually put and remove values when a player joins or leaves. There are also arrays which are similar but.

Some helpful videos:

a basic way of what it would look like:

local States = {}

game.Players.PlayerAdded:Connect(function(Player)
  States[Player.Name] = "Idle"
end)

2 Likes

So, basically if you were to define a global table like

_G.blocking = {}
game.Players.PlayerAdded:Connect(function(plr)
    _G.blocking[plr.Name] = false
end)

And when a player is blocking you set their username on blocking to true like this (assuming the name is a variable)
_G.blocking[name] = true
and you set true to false if theyre not.

EDIT: Global variables can be accessed through any server script.

1 Like

Here’s a guide I’ve posted for this. Module scripts are superior to attributes since you can store values that don’t have an attribute counterpart and more importantly create callable functions within the same module script.

2 Likes

_G is not commonly used, I’d use a ModuleScript instead.

2 Likes

Why would you need global variables when you have a module script’s? _G is not recommended at all anymore.

1 Like

You and @Dynamite2479 both have good points. I forgot about that haha

1 Like

I would recommend using modules, makes everything easy to access. I would also recommend using the Player’s Character so Npcs can function.

local States = {}
States[Player.Character] = {}

return States
1 Like