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

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

Asked by 9 years ago
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)
0
Edited funyun 958 — 9y

1 answer

Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

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)
0
Can you explain? I thought that what I had was: 40 (where it starts) 36 (where it ends) -.5 (the increment of movement) DarwinYork 85 — 9y
Ad

Answer this question