Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Applying force equation onto a Vector3 ?

So today I was working on making a simple function that can apply the correct amount of force onto a cart I made and I had no errors with it when I tested my code, there is a BodyForce object within the car model that has its value changed according to the function and I don't understand why it isn't working correctly.

Force equation: Force = Mass * Acceleration

Also, I have my car within a model, and all the parts within that models are normal parts (No unions involved.)

Here is my code:


-- Variables
local Acceleration = Vector3.new(4.905, 0, 0)
local BodyForce = script.Parent.BodyForce
local Model = game.Workspace.Car
local Mass = 0

--Get mass
for _, component in pairs (Model:GetChildren()) do
	if component:IsA("BasePart") then
 		Mass = Mass + component:GetMass()
end
end
--Apply it to the BodyForce
BodyForce.force = Acceleration * Mass

3 comments

0

Any errors in output?

0

Nope, I don't think it calculated it correctly. I don't know what values to insert for Acceleration to make it move by 1 stud

0

Acceleration is distance per second per second, or m/2^2... I don't understand how you're trying to apply it to a vector3.

1 answer

Functions don't actually run unless they're called upon.

function add5(x) --This initially won't run, it's just the definition of the function.
	return x + 5
end

print(add5(7)) --Here we're actually calling the function and printing what it returns.

function spam(iterations) --This won't run, it's the definition.
	for i = 1, iterations do
		print("spam")
	end
end

spam(10) --Here, we're calling the function. This will make the function run.

By the looks of it, you don't actually need functions in your script.

-- Variables
local Model = nil --Define "Model" here.
local Acceleration = Vector3.new(0, 0, 0) --Set to what you'd like.
local BodyForce = script.Parent.BodyForce
--Also, it's best that you set maxForce at infinity, if you haven't already.
BodyForce.maxForce = Vector3.new(math.huge, math.huge, math.huge)
local Mass = 0

--Get mass
for _, component in pairs (Model:GetChildren()) do
	if component:IsA("BasePart") then
		Mass = Mass + component:GetMass()
	end
end

--Apply it to the BodyForce
BodyForce.force = Acceleration * Mass

2 comments

0

The code did not work correctly.

0

I will edit my post with the updated code

This discussion is preserved as it was. New answers, comments, and votes are closed.