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

What's wrong with this script?(The part moves like 300 studs everytime I click)

local door = workspace.door1

script.Parent.ClickDetector.MouseClick:connect(function()
	for i = 40, 36, -.5 do
		door.Position = door.Position + Vector3.new(i, 0, 0)
		wait()
	end
	wait()
end)

1 comment

0

Edited

1 answer

Well, that's what you're asking it to do!

sum = 0

for i = 40, 36, -.5 do
	sum = sum + i
end

print(sum) --342

This is equivalent to:

sum = 0
i = 40
sum = sum + i
i = 39.5
sum = sum + i
i = 39
sum = sum + i
--...
print(sum)

See the pattern? you're adding 40 and 39.5 to get 79.5. You then add 39 to 79.5 to get 118.5. Eventually, you have a sum of 342. So, your door moves about 342 studs out. Let's change that:

local door = workspace.door1

script.Parent.ClickDetector.MouseClick:connect(function()
    for i = 1, 9 do
        door.Position = door.Position + Vector3.new(1, 0, 0)
        wait()
    end
    wait()
end)

1 comment

0

Can you explain? I thought that what I had was: 40 (where it starts) 36 (where it ends) -.5 (the increment of movement)

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