Ad
Log in to vote
0

Vector.3 and wait help?

Asked by 7 years ago

So Im a really new scripter and I was having some trouble. When I did this function, the first line of the fuction worked, but the 3rd line never was executed to return the brick to its original location. So, can someone help me fix this?

function open()
workspace.Door.Position = Vector3.new(-27.5, 17.7, 21.3)
wait(3)
workspace.Door.Position = Vector3.new(-27.5, 12.8, 21.3)
end

script.Parent.ClickDetector.MouseClick:connect(open)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18066 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

This should work, with a few nuances.

First, you should use CFrame instead of Position.

When you set Position of an object, it makes sure there's open space for it. If not, it sets the object where there is open space (looking upward).

CFrame ignores that requirement and moves the object anyway.

You can make a CFrame using a Vector3, or just by giving it the x, y, and z coordinates.

However since CFrame also describes rotation, it would be better to make this all relative.

First, your script using a variable to make it relative to where the door starts (no more having to know exactly the position of the door!)

local home = workspace.Door.Position
function open()
    workspace.Door.Position = home + Vector3.new(0, 4.9, 0)
    wait(3)
    workspace.Door.Position = home
end

script.Parent.ClickDetector.MouseClick:connect(open)

And now with CFrame:

local home = workspace.Door.CFrame
function open()
    workspace.Door.CFrame = home + Vector3.new(0, 4.9, 0)
    wait(3)
    workspace.Door.CFrame = home
end

script.Parent.ClickDetector.MouseClick:connect(open)

Now there's just the small detail of open being called repeatedly because of multiple clicks. It will shut according to the first click, rather than the most recent one. We can implement something that sort of looks like a debounce to fix this.

local home = workspace.Door.CFrame
local lastPress = nil
function open()
    unique = {}; -- or `math.random()` if that makes more sense
    lastPress = unique
    workspace.Door.CFrame = home + Vector3.new(0, 4.9, 0)
    wait(3)
    if lastPress == unique then
        -- ONLY do this if the last press was MINE
        -- Otherwise, its THEIR job to clean it up
        workspace.Door.CFrame = home
    end
end

script.Parent.ClickDetector.MouseClick:connect(open)
Ad

Answer this question