Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I undo multiple exponents?

Asked by 3 years ago

The more descriptive question is, How can I code it so that whenever I pickup an item it cuts my health in half, but I should be able to set it to any value and it should work?

I have an item I pickup that cuts my health in half. The only issue I currently I have is that I want it to be settable. For example lets say the player has 100 HP, I set their item to 2, their HP is now 25 as it is being cut in half 2 times.

I also want it to be decreased as well, lets say the player has 5 of this item, I can set it to 1 and it would be as if they only had 1 item.

Both of the features don't work with the code I'm using. Here is the code right now:

Before reading it, just know that the base health is the health of the humanoid. The last value is the value before it was picked up, and the child.Value is the value now. The thought behind it was that in the first parentheses I would undo the previous items, then after that I would set it as normal.

                    if name == 'SharpGlasses' then
                        --Cuts health in half
                        baseHealth.Value = (baseHealth.Value * 2 ^ lastValue) * 0.5 ^ child.Value

                    end

Here are the problems that come up from this code: If you pick up 4 of the item, not setting it, but one at a time, it sets your hp to 6. But if you set the item to 4, your hp gets set to 7. Here is the issue in the video below.

(You might have to copy and paste it as I was having issues just clicking on it)

https://imgur.com/a/Zgsj16i

If you set it instead your HP becomes 7. As in I go into the server viewer and then set the item value manually.

Another issue, if you drop the item your HP doesn't become the original value. Here is the video representing the issue:

https://imgur.com/a/aehqIKR

As you can see after dropping the item the HP is now 112 instead of the starting 100. Also, I know for sure that this isn't the dropping function fault as all it does is subtract the drop amount from the current amount and sets it to the value.

Any help would be greatly appriecated!

1 answer

Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

hey there, it's going to be impossible to get perfect values because your health is being constantly rounded all over the place.

one thing I can say for certain about your second problem is that your extra health is happening due to a rounding error. what the player's health really should be is 6.25, but now it's 7. notice that 7 * 2^4 is 112.

to tell the truth, I am not completely certain why you are getting either 6 or 7 for your first issue, but that probably has something to do with a rounding error as well.

my suggestion is that perhaps you can try storing an extra value for the player's "true" health, in decimal form, which you use for all your calculations. then, whenever you set the player's health, simply round that value.

edit: another note, if you have more items that tamper with the players' health, unless this is intentional, simply doing reverse operations won't work. imagine if the player heals after taking damage, then you multiply their hp back with reverse calculations; it will be above what you intended. you might want to just make it simpler and set a hard multiplier against their maximum hp.

0
Thank you so much!!! I forgot that the value was rounded. The base health value was at first an Int Value, but to fix it all I had to do was change it to a number value. TY movementkeys 163 — 3y
0
I was play testing it and you are right, if I have items that alter the players health it is way above the before health. Can explain what you were saying about a hard multiplier? movementkeys 163 — 3y
1
sure. so you'll want to always start at their initial health at the beginning of the game, so 100. every time you change their health, also get the % ratio of their current health to current max. next, recalculate by multiplying starting health by your multiplier, 0.5^n to get their "adjusted max health". multiply that value by the original ratio, then make any changes. Speedmask 661 — 3y
0
I think it will work! Thanks again. movementkeys 163 — 3y
Ad

Answer this question