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

Why doesn't this move my part?

Nothing comes up in output. Used test mode(F6). I'm pretty sure it has something to do with the number of ends I have, but every time I added one it said it was incorrect.

b = workspace.Union.Position.Y

function a()
	if b == 13 then
		repeat
			wait(.5)
			b = b + 1
		until
			b == 27
	else
		repeat
			wait(.5)
			b = b - 1
		until
			b == 13
	end
end

function onClick()
	a()
end

script.Parent.MouseButton1Up:connect(onClick)

3 answers

✓ Accepted answer

Two mistakes: To assign position to a part, you have to use a Vector3 or CFrame value. They x, y and z values of a Vector3 are read-only, meaning you can't manipulate them individually. Second, variables are references, they are not actually the instance. What you are doing is assigning b as the value of y, instead of assigning it as y itself. This means that instead, use b as a reference to the part, and manipulate it's position by obtaining it through the variable: b = workspace.Union

Here's the complete code:

b = workspace.Union

function a()
    if b.Position.y == 13 then
        repeat
            wait(.5)
            b.Position = Vector3.new(b.Position.x,b.Position.y + 1,b.Position.z)
        until
            b.Position.y == 27
    else
        repeat
            wait(.5)
            b.Position = Vector3.new(b.Position.x,b.Position.y - 1,b.Position.z)
        until
            b.Position.y == 13
    end
end

function onClick()
    a()
end

script.Parent.MouseButton1Up:connect(onClick)

1 comment

0

Also, you should use greater than/less than or equal to when using loops. That way, when using smaller increments, the code is much more likely to work.

The first problem with this code is that you are creating a variable that is equal the the Y position. This variable is not associated with the actual position. Changing this variable will not change the actual position.

Second the y property of a Vector3 is read only. You cannot modify it.

Third the y property is in lowercase.

Finally I cleaned up the code and added a debounce.

b = workspace.Union

function ChangeY(y)
	b.Position = b.Position + Vector3.new(0,y,0)
end

function a()
	local db = true
	if db then
	db = false
		if b.Position.Y == 13 then
			repeat
				wait(.5)
				ChangeY(1)
			until
				b.Position.y == 27
		else
			repeat
				wait(.5)
				ChangeY(-1)
		until
			b.Position.Y == 13
		end
	db = true
	end
end

script.Parent.MouseButton1Up:connect(a)

I don't want to give wrong info due to me being an amateur in scripting but you should assign a cframe and add to the part a gyro or BodyPosition. I don't have the program on me right now but ya ill take a look for more specifics

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