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

Why is my block not moving 1 stud like I coded it do so?

Asked by
Peeshavee 226 Moderation Voter
7 years ago

Basically, I have a brick, that when clicked, another part moves. Here's the code:

local p = script.Parent
local db = false


function move(hit)
    local human = hit.Parent
    local b = game.Workspace.boop
    if human then
        if not db then
            db = true
            for i = 0,1,0.1 do
            b.CFrame = b.CFrame + Vector3.new(0,i,0)
            wait(0.1)
            end
            db = false
        end
    end
end

p.ClickDetector.MouseClick:connect(move)

Everything works, but just that it doesn't move 1 stud. It moves like 20 or something. When I put

for i = 0,1,0.1 do

I thought it meant from "0" to "1" going up "0.1" each time. What am I missing here? All help is appreciated!

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago
  1. code line 12:

    b.CFrame = b.CFrame + Vector3.new(0,i,0)

You are adding "i", so "i" is going will increase each iteration of the loop, try this:

b.CFrame = b.CFrame + Vector3.new(0,0.1,0) --0.1 instead of "i"

or you can save the initial position, and then add "i"

local initialPosition= b.CFrame
for i = 0,1,0.1 do
    b.CFrame = initialPosition + Vector3.new(0,i,0)
end
Ad
Log in to vote
0
Answered by 7 years ago

You have told the brick to move 0.1 studs, then 0.2 studs, then 0.3 studs...etc...

This means that the total number of studs it will move is 4.5. To fix this problem, change Vector3.new(0,i,0) to Vector3.new(0,0.1,0).

0.1 multiplied by 10 gives you 1. Therefore, the brick will move 1 stud in total!

Answer this question