Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you subtract values in a table?

I have 3 tables h={1,2,3} h2={1,2,3} h3={0,0,0} They are all labeled at the beginning of the script and I want to subtract their values as listed below. but I get the error between h[1] and h2[1] saying that "expected =, got -" And yes, it's in a function, do I have to define it again?

	h[1]-h2[1]=h3[1]

2 answers

✓ Accepted answer

Let's imagine you have three variables.

number = 15
one = 1
two = 2

Let's say we're going to make number into one:

number = one

And then change it into two:

number = two

Our whole program looks like this then:

number = 15
one = 1
two = 2

number = one
number = two

So the question is... What is the value of one at the end of that?

It's not 15 -- we never assigned to one -- one was never on the left side of an equals sign.


The variable we are changing is always on the left. We can't pick-and-choose where we use the left or the right, because code like the above is ambiguous would be ambiguous when there's one variable on each side.


Flip your assignment around:

h3[1] = h[1] - h2[1]

Now the first element of h3 is the difference between the first elements of h and h2.


As a note, it might be a little better to use the name h1 instead of just h since the others use numbers. What might be better is to use distinct letters, like f g h or a b c.

1 comment

0

Thanks, H was just an example, I have other variable names neatly organized.

You have to flip the 2 things on both sides of the equal sign. When you set a value to something else, the proper syntax is:

**Value being changed = Value of change**

So the correct way to write your expression would be:

h3[1] = h[1] - h2[1]

Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.