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

How is this door script making the door slide faster?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

So I made this script to where a door can slide up slowly but not too slow and when it keeps sliding up, it gets faster and faster. But I am not wanting it to go faster and faster, I want it to stay at a constant speed.

        local Door = game.Workspace.Door
        for i = 0,.2,.001 do
            Door.CFrame = Door.CFrame + Vector3.new(0,i,0)
            wait()
            print(i)
        end

1
... You can just replace index with a number instead ._. Im_Kritz 334 — 8y

2 answers

Log in to vote
3
Answered by 8 years ago
Edited 8 years ago

What is wrong?

The door is rising faster because i is stacking up every time.

If the starting position was 0, 0, 0:

loop #1: It adds 0 .001 studs

loop #2: It adds 0.002 studs, making it's position 0, 0.003, 0

loop #3: It adds 0.003 studs, making it's position 0, 0.006, 0

See where I am going? Instead of rising just 0.001 every time, it is rises the amount of i for every loop, meaning it will rise more every time - making it in turn, appear faster.


How do you fix it?

Simple, you just add a set amount every time:

local Door = game.Workspace.Door
local Step = 0.001 -- how much it rises, so you don't have to define it twice

for i = 0,0.2,Step do
    Door.CFrame = Door.CFrame + Vector3.new(0,Step,0)
    wait()
end 

Hope I helped!

~TDP

0
Well said buoyantair 123 — 8y
0
As another note: avoid using non-integer steps (or rather, steps which cannot be represented as (a/2^b), where a and b are both integers, and b is small) as floating point errors give unexpected results. SwardGames 325 — 8y
Ad
Log in to vote
-1
Answered by
Sur4y 40
8 years ago

You can use BodyVelocity.

0
I am wanting a part to go inside another part though, will it still work? FiredDusk 1466 — 8y
0
If you have collisions disabled, then it should work. Sur4y 40 — 8y
0
If you have collisions disabled its not much of a door, is it? SwardGames 325 — 8y

Answer this question