Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to check if a Vector3 Value has changed?

Asked by 6 years ago

I am making a car. Here is the code.

local tractor = script.Parent
local stats = tractor.Configurations
local v = tractor.VehicleSeat

local bodyPos = tractor.Chassis.BodyVelocity
local bodyGyro = tractor.Chassis.BodyAngularVelocity

local runService = game:GetService("RunService")
local lookVec = nil



tractor.VehicleSeat.Changed:Connect(function(property)
    if property == ("Steer") then
        bodyGyro.AngularVelocity = Vector3.new(0,v.Steer*-5000,0)
        lookVec = v.CFrame.LookVector
    elseif property == ("Throttle") or lookVec--here I need help-- then
        lookVec = v.CFrame.LookVector
        print(v.Throttle)
        bodyPos.Velocity = lookVec * 16 * v.Throttle
    end

end)

As you can see, I just put "or lookVec", but how do I find if lookVec's values have changed? Your Breakfast, TheFierceWaffle

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

This is kind of hacky but what you can do is make another variable called lastLookVec which in the loop we will set to the value of LookVec before we change it and then compare it to see if it changed in the next loop iteration. Instead of doing a while loop you could also use heartbeat in run service but for simplicity I will use a while loop. Heres and example.

local lookVec = nil
local lastLookVec = nil
local lookVecChanged = false -- variable which we will set to show if it changed.
while true do
    wait()
    lookVecChanged = not lastLookVec == lookVec -- this will show wether or not the lookVec is different from the old one. If it is different then lookVecChanged will be true(that is why the not is used).
    lastLookVec == lookVec -- this will make the lastLookVec variable the current lookVec so that in the next loop we can compare it to the value of lookVec and see if it has changed
    if lookVecChanged then -- you could this to know if it changed in the situation you described above.
        print("lookVec has changed!")
    end
end

Hope this helps. In my opinion this technique is kind of hacky and best to avoid. Also if you are using two while loops you will have to put one of them into a new thread so they will both run at the same time. I'm not exactly sure what you are doing so If you find you don't need to use this, definitely don't use this. Good luck.

0
This helps very much! It is very "hacky", but what must be done must be done! TheFierceWaffle 64 — 6y
Ad

Answer this question