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

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

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

✓ Accepted answer
  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

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!

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