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
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