Search code examples
luaroblox

Reset part to original position in roblox


I have a ball and an anchored wall. If i push the ball to the wall and the ball connects with the wall, the ball will reset back to its original position. Below is the script i have for the ball.

local ball = script.Parent
local wall = workspace.Wall

function reset(part)
    ball.Position = Vector3.new(124.5, 1.5, 133.5)
end

wall.Touched:Connect(reset)

However when the ball resets back to its original position, the ball continues to move in the direction that was pushed earlier. How do i make the ball stop exactly in the original position, and only move when the user pushes it again


Solution

  • Just set it's linear and angular velocities to a zero vector.

    local ball = script.Parent
    local wall = workspace.Wall
    
    function reset(part)
        ball.Position = Vector3.new(124.5, 1.5, 133.5)
        ball.AssemblyLinearVelocity = Vector3.zero
        ball.AssemblyAngularVelocity = Vector3.zero
    end
    
    wall.Touched:Connect(reset)
    

    You may also want to check if a part which touched the wall is a ball and nothing else.

    local ball = script.Parent
    local wall = workspace.Wall
    
    function reset(part)
        if part == ball then
            ball.Position = Vector3.new(124.5, 1.5, 133.5)
            ball.AssemblyLinearVelocity = Vector3.zero
            ball.AssemblyAngularVelocity = Vector3.zero
        end
    end
    
    wall.Touched:Connect(reset)