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

How is this door script making the door slide faster?

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 comment

1

... You can just replace index with a number instead ._.

2 answers

✓ Accepted answer

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

2 comments

0

Well said

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.

You can use BodyVelocity.

3 comments

0

I am wanting a part to go inside another part though, will it still work?

0

If you have collisions disabled, then it should work.

0

If you have collisions disabled its not much of a door, is it?

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