Getting a value in a table with a number between 2 of the values

I cant really explain this, but for example: rpm is 1436, it would get the value for 1000 rpm, and if its 1654 for example, it would get the value for 2000 rpm

local timingCurve = { -- Delay in milliseconds, delays the combustion after TDC. Example: [rpm] = <ms>
	[0] = 3,
	[1000] = 3,
	[2000] = 6,
	[3000] = 9,
	[4000] = 12,
	[5000] = 15,
	[6000] = 18,
	[7000] = 21,
	[8000] = 24,
	[9000] = 27
}

local function getRPMFromRadsPerSec(x: number)
	return (x * 60) / (2 * math.pi)
end

RunService.Heartbeat:Connect(function(dt)
	local rpm = getRPMFromRadsPerSec(flywheel.AssemblyAngularVelocity.X)
	if cylinders[1].Head.Position.Y >= firingHeight then
		
	end
end)

Round it up to the value your looking for.

how would i do this? ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

RunService.Heartbeat:Connect(function(dt)
	local rpm = getRPMFromRadsPerSec(flywheel.AssemblyAngularVelocity.X)
	if cylinders[1].Head.Position.Y >= firingHeight then
		rpm = math.round(rpm)
        print(timingCurve[rpm])
	end
end)

alright, ill try this outㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

this doesnt really work, it gives me nil most of the time
for example: it gives me nil when the rpm is 1 but not when its 0

Can you give me a output screenshot.

Ok, you wanna put a if statement.

EDIT:
Like this

if rpm > 0 and not timingCurve[rpm] then
     --whatever you want
end

what do i put in the if statement?

nothing. Just do a elseif statement also.

local timingCurve = { -- Delay in milliseconds, delays the combustion after TDC. Example: [rpm] = <ms>
	[0] = 3,
	[1000] = 3,
	[2000] = 6,
	[3000] = 9,
	[4000] = 12,
	[5000] = 15,
	[6000] = 18,
	[7000] = 21,
	[8000] = 24,
	[9000] = 27
}

local function getRPMFromRadsPerSec(x: number)
	return (x * 60) / (2 * math.pi)
end

RunService.Heartbeat:Connect(function(dt)
	local rpm = getRPMFromRadsPerSec(flywheel.AssemblyAngularVelocity.X)
	if cylinders[1].Head.Position.Y >= firingHeight then
		rpm = math.round(rpm)
		if rpm > 0 and not timingCurve[rpm] then
			--whatever you want
			print(false)
		elseif rpm > 0 and timingCurve[rpm] then
			print(true)
		end
		print(timingCurve[rpm])
	end
end)

EDIT:
Note that 0 will print false.

If all the keys inside your table remain exact multiples of 1000 you can use rounding:

local curve = timingCurve[math.round(rpm/1000)*1000]

else if the keys are random but your table remains small you can brute-force the result using linear search:

local function rpmToCurve(rpm)
	local closest = nil  
	for key, value in pairs(timingCurve) do 
		local distance = math.abs(rpm-key) 
		if not closest or distance < math.abs(rpm-closest) then 
			closest = key 
		end 
	end
	return timingCurve[closest]
end

local curve = rpmToCurve(rpm)

else if you plan to use a much larger table you may want to use a modified version of the binary search algorithm that doesn’t look for an exact match.

1 Like

I already got the answer yesterday but im marking this as a solution because local curve = timingCurve[math.round(rpm/1000)*1000] is what I did.