# Change A Value Based On Altitude

I’m working on a Realistic Re-Entry script. I need to add Air density from 1.225 on the ground to 0 in space. Space in my game is about 600,000 studs up. So how can I change the value of the air density as I get higher up? And if I go back down it changes again.

You could probably make it more realistic using real physics, but you could just divide 1.225 by 600,000 and then decrease air density by that increment per stud.

``````local increment = 1.225/600,000

local density = 1.225 - (increment*height)

--e.g
local height = 400,000
density =  1.225 - (increment*height)

-- gives 0.408 as the air density.
``````
2 Likes

Will try now, don’t have the knowledge to make realistic physics just yet. So ulness you want to exhaust hours helping me then I plan to in the future. Also the value always stays the same.

I’m not sure what you mean. Could you explain how it stays the same?

For example, if you used it in this format:

``````local increment = 1.225/600000

function getDensity(height)
if height > 600000 then
return 0
else
return 1.225 - (increment*height)
end
end

print(getDensity(400000))
``````

Not sure if I’m being stupid. Put this code in and slapped the print function in a while true loop. Value stays the same. Maybe I need to put the function in a while true? I need the value to constantly update. I added the script underneath a part. Is this a server script thing?

Yeah the function won’t update by itself. It will need to be called each time you need a result. So as long as you have the height (y axis) of the object it will run fine.

1 Like