How do I update friction

How do I set a part’s Friction and FrictionWeight while maintaining it’s already pre-existing physical properties? The documentation for physicalProperties is lacking or maybe it’s not possible.

I am not sure how to read a part’s existing physical properties. Some things like part.Friction exist, but things like part.Density or ElasticityWeight do not. part.PhysicalProperties throws an error, and part.CustomPhysicalProperties is nil before being set.

You can read a part’s physical properties through part.CustomPhysicalProperties[property]. The only time you can’t do this is when the part has no CustomPhysicalProperties, in which case you need to use:

part.CustomPhysicalProperties = PhysicalProperties.new(Part.Material)

Here’s a quick function that lets you change only specific properties easily

local function changePhysicalProperties(part, propTable)
	if not part:IsA("BasePart")then
		error("1st argument must be a BasePart")
	end
	if type(propTable) ~= "table" then
		error("2nd argument must be a table")
	end
	
	if not part.CustomPhysicalProperties then
		part.CustomPhysicalProperties = PhysicalProperties.new(part.Material) -- uses the material's physical properties as a basis for the part
	end
	local orig = part.CustomPhysicalProperties
	
	local density = propTable.Density or orig.Density
	local friction = propTable.Friction or orig.Friction
	local elasticity = propTable.Elasticity or orig.Elasticity
	local frictionWeight = propTable.FrictionWeight or orig.FrictionWeight
	local elasticityWeight = propTable.ElasticityWeight or orig.ElasticityWeight
	
	part.CustomPhysicalProperties = PhysicalProperties.new(density, friction, elasticity, frictionWeight, elasticityWeight)
end

local newProperties = {Friction = 0.3, FrictionWeight = 0.7} -- set up a dictionary for the properties
changePhysicalProperties(script.Parent, newProperties) -- use your part instead of script.Parent
12 Likes

Thank you. Though I am very sad that my game will need 24 lines of code to change 2 properties.

4 Likes

if you don’t want type checking and are only going to change friction and friction weight you can just shorten it to this

local function changeFriction(part, friction, frictionWeight)
	if not part.CustomPhysicalProperties then
		part.CustomPhysicalProperties = PhysicalProperties.new(part.Material) -- uses the material's physical properties as a basis for the part
	end
	local orig = part.CustomPhysicalProperties
	friction = friction or orig.Friction
	frictionWeight = frictionWeight or orig.FrictionWeight
	part.CustomPhysicalProperties = PhysicalProperties.new(orig.Density, friction, orig.Elasticity, frictionWeight, orig.ElasticityWeight)
end

changePhysicalProperties(--[[part here]], --[[friction here]], --[[frictionweight here]]) -- put nil for friction if you don't want it to change
6 Likes

Great solution, just to correct your shortened version you have the function named changePhysicalProperties when it’s listed as changeFriction for anyone also hoping to use this.

9 Likes