How to get the total mass of a model and print into output?

Im currently scripting a locomotive and I want to try and find out how much power to give it (via a value) But all my parts have mass in them and the mass values are different.

Basically what im trying to do is get the mass of every part, whether a mesh part, union, base part, any part that has a Mass property, and add up the numbers to get a total mass to print into the output. How would I go about doing so?

I can tell i’d need some sort of function that gets the locomotive model and all parts in it that is some sort of basepart that has a mass property (But consider, scripting is not my strong suit, but heres what I have so far, its not much though), I dont really know what im doing when it comes to stuff like this (its a shame im the only developer in my game lol)

local locomotive = script.Parent

function getMass(locomotive)
	
	for i, v in pairs(locomotive) do
		if i:IsA("BasePart") then
			
			BasePart.Mass = "I dont even know what im doing"
			print(totalmas?)
			
		end
	end
	
end

If anyone can help or has some useful solution please reply! (Edit: This script is in a local script located inside of the locomotive model

1 Like
local locomotive = script.Parent

function getMass(model)
	
	for i, v in pairs(model) do
		if i:IsA("BasePart") then
			
			print(BasePart:GetMass())
			
		end
	end
	
end

i think getmass was an api

The AssemblyMass property on a part gives you the total mass of the structure that it is apart of. I’m pretty sure roblox determines this by checking if a part is connected via welds or joints/motor6ds. For Ex: using Assembly Mass on the humanoid root part will give you the mass of the entire character and everything attached to them

2 Likes

There is a GetMass method for baseparts. You can use this in a for loop to add up the mass of each basepart in a model like so:

function getMassOfModel(Model)
	local TotalMass = 0
	
	for _, descendant in (Model:GetDescendants()) do
		if descendant:IsA("BasePart") then
			TotalMass += descendant:GetMass()
		end
	end
	
	return TotalMass
end

print(getMassOfModel(script.Parent))
1 Like

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