Help with math equation

plr.Character.CharacterClient.Overcharge.Value=math.clamp(plr.Character.CharacterClient.Overcharge.Value + 10,0,100)

I assume this bit is inside a key press event that looks something like this:

local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.Keyboad then
        if InputObject.KeyCode == Enum.KeyCode.LeftAlt then
            plr.Character.CharacterClient.Overcharge.Value=math.clamp(plr.Character.CharacterClient.Overcharge.Value + 10,0,100)
        end
    end
end)

Above, whenever the left alt key is pressed the value of overcharge is increased by 10, but canā€™t be greater than 100.

Then we reach

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)

Here, we set x, or ā€œstrengthā€ Iā€™ll call it, to power + overcharge, and the sum of that value also cannot be greater than 100

You then tween overcharge to (power+Overcharge.Value)%100. Itā€™ll change the value of Overcharge until it reaches whatever (power+Overcharge.Value)%100 is, or however much power+Overcharge.Value is over 100 (note that itā€™s technically some multiple of 100, but we wonā€™t hit that unless both power and Overcharge.Value are 100). There isnā€™t anything that can interrupt the tween.

If power is whatā€™s changing, why arenā€™t you tweening power?

I have a loop inside a localscript that changes GUI sizes based on these values. The inputbegan is in a clientscript, that then fires a RemoteEvent. In that remoteā€™s OnServerEvent, the overcharge tween part occurs and the power is changed. The tween is for visual effects, but it is also necessary in order to reset the Overcharge value. Power must be INSTANTLY changed, so im not tweening it.

How does this occur? Character requirement moment

Itā€™s just a simple Mouse.Button1Down.

So itā€™s only a click, not a click and hold?

No, it is a click and hold. Release Event occurs on Mouse.Button1Up.

Ok.

Power is defined by the /\t while the user is holding the mouse.

Overcharge is initially cap-power, but

Then when mouse input (I assume) begins

By doing

and overcharge changes to

which, in this case, is however much over 100 x is, assuming 100 <= x < 200. The above describes a meter that increases power until MB1 is lifted, then creates a visual effect showing the change between current overcharge and how much power+overcharge is over 100, but this doesnā€™t seem to be what youā€™re looking for.

How does this differ from what youā€™re looking for? I apologize if Iā€™m getting a bit repetitive with the questions, Iā€™m doing my best to understand. I unfortunately donā€™t know much about FIFA, but did my best in searching stuff online.

We have 2 values. Power and overcharge. They are both capped to 100 using math.clamp(). I then add overcharge to power and cap that to 100. That means if both values were 100 (combined they are 200), math.clamp reduces Power to 100. In short, power + overcharge now equals 100.

Moving on, overcharge is set to 0. But sometimes, we dont want it to be 0, because were not using all of our overcharge meter. Sometimes were only using 91 of it. So instead of setting it back to 0, we set it back to 9, because thats what is left over from power+overcharge (NOTE THAT IT IS CAPPED TO 100.). How do i get that number that is left over. The modulus solution you provided is supposed to work as I am technically trying to find how much above 100 we are, but it doesnt.

Hopefully this is a much more clear explanation of my problem

Thatā€™s much more clear, just one more question (hopefully) that I shouldā€™ve asked a long time ago (my fault): Based on the name, I assumed overcharge was how much over 100 the power meter was, but what exactly is overcharge and how does it work?

Many players in my game wanted to be able to shoot without having to hold down their MB1 for 1 second to charge it up. So, I added overcharging. The name doesnt really suit the ability well though. Basically if you have no power meter charged up then overcharge will come into play and shoot with how much you have your overcharge bar up.

Ok, there are two separate bars.

  • The power bar is increased by holding down MB1, and can only be filled to 100.
  • The overcharge bar can be increased by pressing LeftAlt, and it also caps at 100. It serves as a supplement to the power bar to reduce time spent holding MB1.

When a player shoots, you want to add some of the overcharge bar to the power bar to fill it to 100, and when that happens, you want a visual decreasing the overcharge bar to its new value. That new value will be how much power is over 100 as a result of having added overcharge to it.

--Power defined somewhere up here
local oc = script.Parent.Overcharge.Value
local x = Power + oc

--if x is greater than 100, drain overcharge by amount used
--otherwise, set it to 0 because overcharge is completely drained
oc = (x > 100) and x-100 or 0
Power = math.Clamp(x,0,100)

And in the special situation you mentioned, you add a special condition, yes?

Can you explain the special situation too?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.