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