How would I create a list editable by other scripts?

I’m currently developing an arcade style combat game in which a character’s attributes can be changed at any given time. I currently have a very hacky solution to the classic walk speed problem, but I’ve come up with an even better solution. The problem is, I have no idea how to implement it.

Essentially, we’ll use the Speed attribute as an example. I want to make it so different scripts from different things like other weapons, traps, debuffs, and other sources can affect this speed attribute.

Let’s say the default Speed Multiplier is 1. The player drinks a cola that grants them a Speed Multiplier of 1.3. But during the cola’s effect, the player steps into a trap that afflicts them with a Speed Multiplier of 0.5.
Now we have two multiplier values we could use. Without outside interference, the Speed attribute would be updated to use the newest value it was fed, which could cause unintended and abnormal behavior.

But my intended solution is to instead feed the two values, 1.3 & 0.5, into a list of current values. Then, whenever this list is updated, we use the lowest of the two values for the Speed Multiplier. And if this list is empty, we’ll use a fallback default value of 1.

I unfortunately don’t know how to implement something like this in practice, which is why I have come here for help. How would I go about creating a list for each character which allows things to insert & remove values, which the main character/server would read from? I assume this would use tables, but perhaps there’s a method using some other feature that I am not aware of.

3 Likes

Use a module script because all other scripts will be able to use it

There are multiple different methods to achieve this.

  1. Use a ModuleScript as already mentioned, however the values will not sync across the client-server boundary.
  2. Use a instances like NumberValue, IntValue, StringValue etc. to hold the required information, making sure they’re accessible to all scripts that need them.
  3. Use attributes on related instances.

If you need or want replication, I would advise against using a ModuleScript.