How to simulate player gravity?

I want to simulate the default’s Roblox gravity (don’t ask why) on the player but I’m not sure what I did wrong.

Here this the code:

function getMass(model: Model)
	local mass = 0
	for i,v in pairs(model:GetDescendants()) do
		if(v:IsA("BasePart")) then
			mass += v:GetMass()
		end
	end
	return mass
end
function getAssemblyMass(model: Model)
	local mass = 0
	for i,v in pairs(model:GetDescendants()) do
		if(v:IsA("BasePart")) then
			mass += v.AssemblyMass
		end
	end
	return mass
end

local gravityForce = Instance.new("VectorForce", rootpart)
gravityForce.Name = "gravityForce"
gravityForce.Attachment0 = rootAttachment
gravityForce.RelativeTo = Enum.ActuatorRelativeTo.World
gravityForce.Force = Vector3.zero

-- this is in a runservice

if not isGrounded() then
	gravityForce.Force += getAssemblyMass(char) * (Vector3.yAxis * 21 * -9.81) * deltaTime -- F = ma
else
	gravityForce.Force = Vector3.zero
end

What am I doing wrong here? Is it the formula? The equation? Any help is appreciated :,)

Is the current gravity set to zero, because if it isn’t, that means you’re just doing double gravity.

Second, what are you trying to accomplish in the first place and why? You’re on the verge of creating an XY Problem: https://xyproblem.info/

I have a VectorVelocity as the VelocityConstraintMode for the X and Z (plane) movement and the Y is 0 so essentially the gravity is non-existent. Only the VectorForce affects the player’s gravity.

I’m not sure how to elaborate on it, but as for the purpose, I want to apply a constant gravity force if the player were to land on a certain surface.

So like you want the players gravity to change depending on the surface they are touching? Have you considered using zones so that when a player is in it, it changes the gravity?

I want the constant downward force of the player’s velocity not when the player touches something

Force is something that should not be applied with +=, it is the change of velocity in time. So it already increases the velocity, you don’t need to increase the force if you don’t want exponential velocity from gravity.

:GetMass() should not be used, prefer .Mass and add not .Anchored and not .Massless to your getMass().
Also don’t fill the second argument of instance.new, parent it in another line.

Have you set the Gravity in studio settings to zero?

I used += because I saw from a video from Unity that adds gravity to a basic character controller, and I assumed that += is like an acceleration.

The unity code goes something like:

 private float _gravity = -9.81f;
    [SerializeField] private float gravityMultiplier = 3.0f;
    private float _velocity;

    #endregion

    private void Awake()
    {
        _characterController = GetComponent<CharacterController>();
    }

    private void Update()
    {
        ApplyGravity();
        ApplyRotation();
        ApplyMovement();
    }

    private void ApplyGravity()
    {
        if (_characterController.isGrounded && _velocity < 0.0f)
        {
            _velocity = -1.0f;
        }
        else
        {
            _velocity += _gravity * gravityMultiplier * Time.deltaTime;
        }
        
        _direction.y = _velocity;
    }

Especially this line
_velocity += _gravity * gravityMultiplier * Time.deltaTime;

Interesting, may I ask why? Does it make any difference?

No, it’s not zero, I do not think that the workspace’s Gravity affects the player as the vector constraint practically cancels out gravity, and I’m just using VectorForce to add the gravity back. I also do not want the workspace gravity to be zero as I plan to add some unanchored parts in the future and I would not like them to float.

He is increasing velocity, not force.

Don’t trust the engine. It might change, or fail you. It was causing some memory leak sometime ago. So even vs code extensions warn you about Instance.new having only 1 parameters, but you set 2 etc.

Hmm, so I’ll use LinearVelocity in line constraint mode instead?

Ah I see, I research and found this thread:

I’ve removed all the 2nd Instance.new parameters and placed them last, I never knew that this was a bad practice :,)

Wdym? If you want to simulate gravity on object that is not grounded for some reason, you just need to do

if isGrounded() then
	gravityForce.Force = Vector3.zero
else
    gravityForce.Force = getAssemblyMass(char) * (Vector3.yAxis * 21 * -9.81)

IF humanoid does not cause problems.

but this wouldn’t be as smooth, almost like a snapping effect