 Studio Auto math.floor

Hello! I am wondering why studio does this issue I’m going to explain, in the game I’m working on there’s a player and enemy, player has damage in a module, and enemy has health as a number value, the issue is when I do subtract the enemies health by the players damage, it automatically sets the value to not be a decimal for instance if the damage was 5.3 and you subtracted it would set it to 5, how would I fix this?

did not add but it does not math.floor it rounds the values!!

math.floor takes any value between 5.0 and 5.9999999 and makes it 5.
math.round makes 5.49999 round to 5 and makes 5.5 round to 6.
If you want one decimal place you can do this to your number variable (roundednumber and number for example):
roundednumber = math.round(number * 10) /10
So if you had a value of 5.332 it would do this:
number = 5.332
multiply it by 10, so 53.32
round that, so 53
divide number by 10, so 5.3
roundednumber would equal 5.3

Are you sure the number value isnt a intvalue?

1 Like

Trying to understand your post a bit better with a second read-through.
As @jbjgang2 asked, did you want the number to be an integer which is a whole number (no decimals) or have a certain number of decimals like my example which gives you 1 decimal place.

No, im trying to get it to not convert to a whole number, so a buff im making actually does something instead of rounding down (also gonna say i want it to be 1 decimal place)

So like @jbjgang2 and I asked, are you using an IntValue or a NumberValue?
Try posting your script, since you are asking for help with a scripting question.

oh my, this entire time I did not know it was a int value, thanks!

1 Like

sorry did not see this (character limit)

1 Like

I know i already got my answer but now i need to know how to do this, i tried by imputing number as the damage and it still does the issue could you help?

Code:
Enemy.Health.Value = Enemy.Health.Value - math.round(Damage * 10) /10

Just do
Enemy.Health.Value = Enemy.Health.Value - Damage

math.round(Damage * 10) /10 is unnecessary, just subtract the damage. I dont think thats what the issue is though.

1 Like

still does the thing, randomly thats the issue at hand, so for instance the damage is 1.1 the enemies health is 4, it does 2.9 correct but then on 1.8 it somehow does 1.7999999 or something like that

That’s due to something called floating point error, which is a fancy way of saying the computer is not that accurate because it’s limited in how many decimal places it can go to.
That’s why I suggested doing it the math.round way.
You also have to consider exactly what value Enemy.Health.Value is. If it’s not a short decimal number then your result may be slightly off. Below is a way that takes any tiny decimal values out of the entire calculation:

If you want 1 decimal place (1.8) to show then use:
Enemy.Health.Value = (math.round(Enemy.Health.Value - Damage) * 10) /10)
If you want 2 decimal places use:
Enemy.Health.Value = (math.round(Enemy.Health.Value - Damage) * 100) /100)
which rounds 1.799999 to 1.80.
If you increase the 2 last values by a zero the decimal value gets longer. 1000 gives you 3 decimal places, 10000 gives you 4 places.

2 Likes

no matter what i try it doesn’t work

code:
local Damage = DamageModule.CalculateDamage(Tower, Module.Data[Tower.Name][“Level”…Level][“ShootType”], Enemy)

--print((math.round(Enemy.Health.Value - Damage) * 100) /100)
local NewDamage = math.round(Damage * 100) / 100 --math.round(Enemy.Health.Value - Damage) * 100) /100
Enemy.Health.Value = Enemy.Health.Value - NewDamage

So you need to try the script in my last post.

If it still doesn’t work then before the calculation print(Enemy.Health.Value) and print(Damage), then after the calculation print(Enemy.Health.Value) to see what the difference is in the Output window.

1 Like

your script somehow automatically rounds it to 1

Rounds WHAT to 1? I know that’s the answer you just got, but that’s why I said in my last post to get the prints of the values before the calculation and after.

It’s like your math teacher telling you that your answer is wrong without explaining why or like only telling a mechanic ‘my car doesn’t work’ and expecting them to know exactly where to look to repair it.

Please please please give us the script you are working with each time so we know what you did to try fixing it, and if we ask many questions in our post then answer ALL those questions, not just one of them.

Try this:

print(Enemy.Health.Value)
print(Damage)
Enemy.Health.Value = (math.round(Enemy.Health.Value - Damage) * 100) /100)
print(Enemy.Health.Value)