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

Why does the part go one extra stud?

Asked by 7 years ago
Edited 7 years ago
local Part = script.Parent -- this is the Part we will move
local newPos = Part.Position + Vector3.new(0,0,8) --the position the Part will go to
local Time = 5 -- the time that the script will take to move the part
local Increment = 3 -- the Part will move 3 studs each time it moves
local Debounce = false

local Diff = newPos - Part.Position -- the difference between the two positions
local Mag = Diff.magnitude -- the distance between the two parts
local Direction = CFrame.new(Part.Position, newPos).lookVector

game.Workspace.FieldMarkers.MyClickDetectorEvent.OnServerEvent:Connect(function()   
    if Debounce then return end -- end the function if debounce is true
    Debounce = true -- make Debounce true so the function can't run
    wait(1.5)
    for n = 0, Mag, Increment do
        Part.CFrame = Part.CFrame + (Direction * Increment)
        wait( (Time/Mag) * Increment )
    end
    Debounce = false -- set Debounce to false so the function can run again
end

)

This script is supposed to make the brick go 8 studs to reach its position, but instead it adds 1 extra stud and I don't know where it added the extra stud and why it adds it.

0
And the positions that are subtracted are not decimals they are whole numbers so that's not the problem InnovativeDesires 0 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

I believe the problem is that the increment is 3, but the target distance is 8. 3 does not go into 8 at all - the closest number to 8 it goes to is 9, which is one more than 8.

To get this working right, you'd want to make the increment something that goes into 8 - for example, 1, 2 and 4 are factors of 8, so they would fit in fine.

You could also use a decimal to smooth out the animation - or even TweenService, which would easily replace this entire script.

local Increment = 2

Hope I helped!

~TDP

Ad

Answer this question