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

Why doesn't this move my part?

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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)

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. aquathorn321 858 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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)
Log in to vote
0
Answered by 9 years ago

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

Answer this question