Calculate rotation to direction is not correct

I am trying to calculate the direction based on the rotation of an object. This is the function I am currently using to calculate the address

function RotationToDirection(fixture, rotation)
    local x = math.rad(rotation.x)
    local y = math.rad(rotation.y-60)
    local z = math.rad(rotation.z)
  
    
    local directionX = -math.sin(y) * math.cos(x)
    local directionY = math.sin(x)
    local directionZ = -math.cos(y) * math.cos(x)

    local tempX = directionX * math.cos(z) - directionY * math.sin(z)
    local tempY = directionX * math.sin(z) + directionY * math.cos(z)

    return vector3(tempX, tempY, directionZ)
end

This code works almost fine, the problem it has is that when the object points backwards it bugs and the direction goes either way.