Help with math equation

In my game i recently implemented a feature called overcharge. If a player kicks the ball with 30 power and they have 10 overcharge then their power would be turned into 40 and overcharge would be 0 because the 10 overcharge is turned into power.
But, if their power is 99 and their overcharge is 10, how would i find how much they would have left?

3 Likes

We’ll need to know what the maximum power value is to solve this though

1 Like

power(30) + overcharge(100) = 130. The maximum power is 100. 130-100 = 30.

2 Likes

Removed the reply because I understood what you want and now working on a solution

@BILLKANAVOS you’ll need a 3rd value to do this reliably like so:

x = power

power = math.min(power + overdrive, 100)

overdrive = power - x

Sorry @BILLKANAVOS but I can’t find a way to reliably calculate the resulting overdrive atm, but my power equation should work correctly at least. If I find a way to correctly calculate the overdrive then I’ll let you know

1 Like

power(30) + overcharge(100) = 130. The maximum power is 100. 130-100 = 30

power + overcharge = x
overcharge = 100 - x  --resets your overcharge.
power = x

In this case, power and overcharge are additive. The strength of your kick is power + overcharge, but the limit is 100. You want to know how much over 100 the player is, correct?

If that is the case, you use modulus:
(power+overcharge)%100

Modulus will give you the remainder after division.

Say power is 100 and overcharge is 30. 100+30 = 130, and 130%100 = 30.
Say power is 99 and overcharge is 10. 99+10 = 109, and 109%100 = 9.
Say power is 90 and overcharge is 10. 90+10 = 100, and 100%100 = 0.
Say power is 30 and overcharge is 10. 30+10 = 40, and 40%100 = 40. (but, of course, verify that power+overcharge is greater than or equal to 100)

(wrong reply my bad)

3 Likes

So the maximum power is 100, you have a power of 30 and an overcharge of 10.
You want to add overcharge to power, and then zero out overcharge. Is that correct?

1 Like

That is correct, but note that the power and overcharge variable can change.

Yes, but that should be ok, right? From my understanding, the power and overcharge would only change after a user kicks the ball again, correct?

If the user kicks a ball, both variables (I assume) start with a value of 0, but are then overwritten to 30 and 10, to use your example. The game will perform its strength calculation and launch the ball. If that’s all correct, when the next user kicks the ball, the values 30 and 10 will still be in place from the previous kick, but it shouldn’t matter since they’ll be overwritten anyway, yes?

No, the power and overcharge can be changed, as it is a FIFA-like power meter. Holding down mousebutton1 will increase the power variable. Releasing it will shoot the ball at the value it was when it was released. Overcharge can be increased by 10 by pressing leftalt.

Ahh I see, so the power increases as you hold down MB1, but it will cap when it hits 100, and you have the possibility of having 10 overcharge if the user is holding left alt, yes?

Yes, but you dont have the possibility to have 10 overcharge. You can increase overcharge by 10 every time you press left alt. Power and Overcharge are capped to 100

In shorter words, i want to add overcharge to power. But, in some special occassions, power might be 91 through 100. 100-91 = 9. So overcharge will be set to 9, as we have 9 spare power.

1 Like

So overcharge is the difference between the power cap and the current power, but can be increased by 10 every time you press left alt. Does that mean whenever you press left alt, you want to limit the power by 10 less? Say we have a power of 91 and a cap of 100. The current overcharge is 9, but the user pressed left alt so the overcharge is now 19. You want to calculate the new power of the ball with 10 more overcharge, which is 10 less than its current value?

If you want to add overcharge to power and if overcharge is the difference between power and the cap, it will always be 100 since cap-power+power=cap

1 Like

No, I just want to add overcharge to power, and if power is now above 100, find how much above 100 we are and set overcharge to that. I tried using modulus just like you recommended, but it did not work. It raised my overcharge value.

1 Like

Can you show me your code? That might help be get a better understanding.

1 Like
local x = Power + script.Parent.Overcharge.Value
Power = math.clamp(Power + script.Parent.Overcharge.Value,0,100)
overcharge = true	
print(Power)
			
task.spawn(function()
	local t = game.TweenService:Create(script.Parent:WaitForChild("Overcharge"),TweenInfo.new(.5,Enum.EasingStyle.Linear),{Value=x%100})
	t:Play()
	t.Completed:Wait()
	t = nil
	gcinfo()
end)

this is the important part of the code.

can I also see the part where you set script.Parent.Overcharge.Value?

plr.Character.CharacterClient.Overcharge.Value=math.clamp(plr.Character.CharacterClient.Overcharge.Value + 10,0,100)
	local HUD = plr.PlayerGui.HUD
	HUD.Overcharge.Visible = true

Is anyone still reading this post? Bumping