Help with math problem

So i’m trying to create like a roblox version of idle miner tycoon, then it’s the thing where I need to make sure it doesn’t take more than the capacity of the elevator. It’s kinda weird but I hope it works.

This is the ui: I think you understand the concept.

The local script:

Elevator.Click.MouseButton1Click:Connect(function()
	if activeElev == false then
		activeElev = true
		while activeElev == true do
			Elevator.Time.BackgroundColor3 = Color3.fromRGB(118, 157, 116)
			Elevator:TweenPosition(
				UDim2.new(0.055, 0, 0.417, 0), -- Starting pos | 0.055, 0, 0.2, 0
				"InOut",
				"Quad",
				0.5,
				false
			)
			wait(0.5)
			Elevator.Time.BackgroundColor3 = Color3.fromRGB(157, 113, 37)
			local shaftStorage = Shaft1.Storage.Value
			if Elevator.Capacity.Value >= Elevator.Stored.Value then
				local available = (Elevator.Capacity.Value - Elevator.Stored.Value)
				local taking = (Shaft1.Storage.Value - available)
				Shaft1.Storage.Value -= available
				Elevator.Stored.Value += available
				local abbreviated = AbbreviationModule:AbbreviateNumbers(tostring(Elevator.Capacity.Value))
				local abbreviated2 = AbbreviationModule:AbbreviateNumbers(tostring(Elevator.Stored.Value))
				Elevator.CapacityTxt.Text = abbreviated2.."/"..abbreviated
			end
			wait(1)
			Elevator.Time.BackgroundColor3 = Color3.fromRGB(118, 157, 116)
			Elevator:TweenPosition(
				UDim2.new(0.055, 0, 0.2, 0), -- Starting pos | 0.055, 0, 0.2, 0
				"InOut",
				"Quad",
				0.5,
				false
			)
			wait(0.5)
			Elevator.Time.BackgroundColor3 = Color3.fromRGB(157, 113, 37)
			wait(0.5)
			plr.hiddenstats.bronze.Value += shaftStorage
			Elevator.Stored.Value = 0
			local abbreviated = AbbreviationModule:AbbreviateNumbers(tostring(Elevator.Capacity.Value))
			Elevator.CapacityTxt.Text = "0/"..abbreviated
			wait(0.5)
			Elevator.Time.BackgroundColor3 = Color3.fromRGB(157, 93, 93)
			activeElev = false
			wait(0.25)
		end
	end
end)

So I believe that this would work but right now it just confuses me…

Any help appreciated.

1 Like

Are there any errors? The script does check for capacity. I do not understand what you expect to happen and what is going wrong with this script. I will note that it doesn’t need to be in a while loop as the loop only runs once.

The thing is that the math isn’t correct, it always takes 1,000 because that’s the capacity, even tho it’s not 1,000 in the “Shaft” that it’s gonna take.

It’s probably because you aren’t using the local taking variable, you should write a couple checks around this value

if Elevator.Capacity.Value > Elevator.Stored.Value then
	local available = (Elevator.Capacity.Value - Elevator.Stored.Value)
	local taking = math.min(Shaft1.Storage.Value, available)
	Shaft1.Storage.Value -= taking
	Elevator.Stored.Value += taking
	local abbreviated = AbbreviationModule:AbbreviateNumbers(tostring(Elevator.Capacity.Value))
	local abbreviated2 = AbbreviationModule:AbbreviateNumbers(tostring(Elevator.Stored.Value))
	Elevator.CapacityTxt.Text = abbreviated2.."/"..abbreviated
end

I recommend using math.min as it will take the smaller of the two, if the shaft’s storage is less than the available space it will take the shaft’s storage. if the available space is less than the shafts storage it will fill the elevator’s available space

This works, but it still adds the total amount of the shaft, so if I have full space available, and the shaft has 1.4k it will then say 1k on the elevator since its the max, but it will give the bronze 1.4k. So it works but it takes more than it should.

I solved it by changing shaft storage here to add to the bronze value, I changed that to take the elevator’s value. And now it gives a maximum of 1k and takes 1k from the shaft. Thank you.

1 Like

That’s because you only add by the shaft’s storage, which is copied when it’s 1.4k. you need to add by the elevator storage.

plr.hiddenstats.bronze.Value += Elevator.Stored.Value
Elevator.Stored.Value = 0